PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000100. Kế hoạch vận chuyển (Transportation Plan)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Kế hoạch vận chuyển (Transportation Plan)

Transportation Plan

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn planets connected by n−1n-1 bidirectional routes forming a tree. Each route has a nonnegative travel time. There are mm transportation plans (u,v)(u,v), each following the unique path from uu to vv. All plans run concurrently, so the completion time is the maximum duration among the plans.

You may choose one edge and change its travel time to zero for all plans. Find the minimum possible completion time.

Input

  • The first line contains n,mn,m.
  • The following n−1n-1 lines each contain a,b,ta,b,t: a bidirectional edge and its travel time.
  • The following mm lines each contain u,vu,v: one transportation plan.

Output

Print one integer: the minimum completion time.

Subtasks

  • Subtask 1 (20 points): 2≤n≤3000002\le n\le 300000, 1≤m≤3000001\le m\le 300000; 1≤a,b,u,v≤n1\le a,b,u,v\le n; 0≤t≤10000\le t\le 1000. Các cạnh tạo thành một cây. Additionally, n≤10n\le 10 and the number of queries/plans is at most 1010.
  • Subtask 2 (30 points): 2≤n≤3000002\le n\le 300000, 1≤m≤3000001\le m\le 300000; 1≤a,b,u,v≤n1\le a,b,u,v\le n; 0≤t≤10000\le t\le 1000. Các cạnh tạo thành một cây. Additionally, n≤2000n\le 2000 and the number of queries/plans is at most 20002000.
  • Subtask 3 (50 points): 2≤n≤3000002\le n\le 300000, 1≤m≤3000001\le m\le 300000; 1≤a,b,u,v≤n1\le a,b,u,v\le n; 0≤t≤10000\le t\le 1000. Các cạnh tạo thành một cây.

Examples

Input

3 2
1 2 4
2 3 6
1 3
2 3

Output

4

Explanation

Setting the travel time of edge 22–33 to zero leaves durations 44 and 00 for the two plans; hence the completion time is 44. Setting the other edge to zero would leave the first plan at duration 66.