#H2026F. Flee from Sight
Flee from Sight
Description
You are given a tree with vertices. Each edge is directed, and its direction represents the line of sight. We say that a vertex can see a vertex if and only if there is a directed path . Note that the direction of an edge only affects visibility, not movement. In other words, if there is a directed edge , you are allowed to move both from to and from to .
You have missions on days. The mission on day is denoted by , meaning that you must move from to . If you reach at time , your mission is considered successful at time and at all subsequent times.
However, a hunter searches for you every day. On day , the hunter follows the simple path from to . After reaching , the hunter remains there until the end of the day.
Each day starts at time , when you and the hunter set out simultaneously from and , respectively. Traversing an edge takes unit of time for both of you. At each time step, you may either move along one edge or remain at your current vertex. The hunter always moves along the specified route unless he has already reached his destination , in which case he remains there. If the hunter can see you at any time, you are discovered and the mission fails.
For each day's mission, find the earliest time at which it can be completed. If it is impossible to complete the mission, output -1.
Format
Input
The first line contains two positive integers , denoting the number of vertices and the number of days, respectively.
Each of the next lines contains two positive integers , denoting a directed edge .
Each of the next lines contains four integers , describing the mission for one day as specified above.
Output
Output lines. The -th line should contain the earliest time at which the mission on day can be completed. If the mission cannot be completed, output -1.
Samples
6 3
1 2
3 2
3 4
2 5
1 6
6 3 2 4
2 4 1 3
1 3 6 5
2
3
-1
12 1
2 1
1 3
1 4
5 3
6 5
2 7
8 6
6 9
10 5
11 9
12 11
12 7 8 4
10
相关
在下列比赛中: