#abc307f. [abc307_f]Virus 2
[abc307_f]Virus 2
Problem Statement
There are rooms numbered , , , , each with one person living in it, and corridors connecting two different rooms. The -th corridor connects room and room with a length of .
One day (we call this day ), the people living in rooms got (newly) infected with a virus. Furthermore, on the -th of the following days , the infection spread as follows.
People who were infected at the end of the night of day remained infected at the end of the night of day .
For those who were not infected, they were newly infected if and only if they were living in a room within a distance of from at least one room where an infected person was living at the end of the night of day . Here, the distance between rooms and is defined as the minimum possible sum of the lengths of the corridors when moving from room to room using only corridors. If it is impossible to move from room to room using only corridors, the distance is set to .
For each (), print the day on which the person living in room was newly infected. If they were not infected by the end of the night of day , print .
Constraints
- All are different.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print lines.
The -th line should contain the day on which the person living in room was newly infected.
Sample Input 1
4 4
1 2 2
2 3 1
2 4 3
3 4 2
1
1
2
3 3
Sample Output 1
0
1
1
2
The infection spreads as follows.
- On the night of day , the person living in room gets infected.
- The distances between room and rooms are , respectively. Thus, since , the people living in rooms and are newly infected on the night of day .
- The distance between rooms and is . Thus, since , the person living in room also gets infected on the night of day .
Therefore, the people living in rooms were newly infected on days , respectively, so print in this order on separate lines.
Sample Input 2
7 7
1 2 2
2 3 3
3 4 1
4 5 1
5 6 3
3 7 1
4 7 1
2
1 6
2
2 3
Sample Output 2
0
1
2
-1
2
0
-1
Sample Input 3
5 1
1 2 5
2
1 3
3
3 7 5
Sample Output 3
0
2
0
-1
-1
Note that it is not always possible to move between any two rooms using only corridors.