
Marin and Luka are playing a popular kids' game called Hide and Seek (Skrivača).
They are playing in their house, which has
Luka has thought of a hiding strategy: when Marin enters room
Marin will find Luka when both of them are located in the same room, and at that moment, the game ends.
Marin found out about Luka's hiding strategy, so he wants you to determine for each starting room if Marin can find Luka in a finite amount of steps and, if he can, determine the least amount of steps necessary if both of them play optimally. (Marin plays such that he finds Luka in the least amount of steps, and Luka plays such that Marin finds him in the most amount of steps).
Input Specification
In the first line, there are integers
In the second line, there are
In
Output Specification
In the first and only line, print -1
if Marin can't find Luka.
Constraints
Subtask | Points | Constraints |
---|---|---|
Luka's hiding strategy will be such that he will never attempt to hide in the same or adjacent room to where Marin is currently located, and the structure of the house will be such that the game can end in at most |
||
No additional constraints. |
Sample Input 1
4 4
3 4 1 2
1 2
2 3
3 4
4 1
Sample Output 1
-1 -1 -1 -1
Sample Input 2
8 9
2 3 2 1 6 5 6 7
1 2
1 3
2 4
3 4
4 5
4 6
6 7
5 7
4 8
Sample Output 2
1 2 2 2 1 1 1 1
Explanation for Sample 2
Marin enters room
Sample Input 3
9 8
1 9 1 1 1 9 9 9 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
Sample Output 3
0 1 1 2 1 1 2 1 1
Comments