Under construction

Still being built. Things may change

Taking Photos

Time limit8sMemory limit1024 MB

Summary
Pick a closed walk through a tree from room 1 that crosses each corridor twice, so each friend's interval covers both of their rooms; minimize the total interval length.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, Bit manipulation, DFS
Solved
No attempts yet

Problem

Seoul Science High School can be represented as a tree of NN rooms and N−1N-1 corridors, each connecting two different rooms. Every corridor can be walked in both directions, and any room can be reached from any other room through the corridors. Passing through one corridor takes time 11.

Bru is about to graduate, and he wants to take photos with KK friends with whom he built memories at Seoul Science High School.

Bru will take two couple photos with each of the KK friends. For the ii-th friend, the photos are taken in room AiA_i and room BiB_i. It is guaranteed that to travel from room 11 to room BiB_i, one must pass through room AiA_i.

The 2K2K photos require moving around the school, but if all friends walk together, it gets crowded. So Bru and his friends decide to move as follows.

First, Bru plans a tour of the school. The tour must satisfy the following conditions.

  • The tour starts at room 11 at time 00 and ends by arriving at room 11 at time 2N−22N-2.
  • Every corridor is passed exactly twice, once in each direction.
  • Time counts only corridor passage. Time for taking photos is ignored.

For convenience, write the tour as a sequence S0,S1,⋯ ,S2N−2S_0, S_1, \cdots, S_{2N-2}, where SiS_i is the number of the room Bru has just arrived at at time ii.

Next, the KK friends choose when to join the tour. The ii-th friend chooses a join time LiL_i and a leave time RiR_i. Friend ii takes photos in rooms AiA_i and BiB_i with Bru, so Bru must visit both rooms during that interval. That is, there must exist integers ee and ff with Li≤e,f≤RiL_i \le e, f \le R_i, Se=AiS_e = A_i, and Sf=BiS_f = B_i.

The travel time of friend ii is defined as Ri−LiR_i - L_i. Bru wants to minimize the sum of all friends' travel times. Find the minimum possible sum of travel times.

Input

The first line contains the number of rooms NN and the number of friends KK.

From the second line to the NN-th line, line i+1i+1 contains the two room numbers UiU_i and ViV_i connected by corridor ii, separated by a space.

From line N+1N+1 to line N+KN+K, line N+iN+i contains the room numbers AiA_i and BiB_i where friend ii takes photos, separated by a space.

Output

Print the minimum possible sum of all friends' travel times.

Constraints

  • 2≤N≤5×1052 \le N \le 5 \times 10^5
  • 1≤K≤181 \le K \le 18
  • 1≤Ui≤N1 \le U_i \le N (1≤i<N1 \le i < N)
  • 1≤Vi≤N1 \le V_i \le N (1≤i<N1 \le i < N)
  • Ui≠ViU_i \neq V_i (1≤i<N1 \le i < N)
  • The given structure of Seoul Science High School is guaranteed to be a tree.
  • 1≤Ai,Bi≤N1 \le A_i, B_i \le N (1≤i≤K1 \le i \le K)
  • Ai≠BiA_i \neq B_i (1≤i≤K1 \le i \le K)
  • To travel from room 11 to room BiB_i, one must pass through room AiA_i. (1≤i≤K1 \le i \le K)
  • All input values are integers.

Examples1

  1. Example 1

    Input
    7 3
    1 2
    1 3
    2 4
    2 5
    3 6
    3 7
    1 4
    2 5
    1 7
    
    Expected output
    5