Yesterday, Henning fell asleep while his chem teacher elaborated on the world of quantum mechanics. Unfortunately, the world of quantum mechanics is closer than he thinks.
The next day, Henning walks into Massey to realize that the school has rearranged itself into a collection of
Henning's head might be a little frazzled by the destruction of physics, but he is not about to call in sick that day, since he has a math test. He needs to walk to his locker at location
Input Specification
On the first line,
On the next
Output Specification
The smallest amount of time in seconds it takes for Henning to walk from the front entrance to his math class while also passing through his locker. If there is no such path, output -1
.
Sample Input 1
5 5 2 4
1 2
2 3
2 5
3 4
5 4
Sample Output 1
3
Explanation for Sample 1
Here is a visual representation, where each numbered circle represents a corresponding location and each line represents a portal. Henning can either walk the path
Sample Input 2
6 4 3 2
1 2
3 4
6 5
4 5
Sample Output 2
-1
Sample Input 3
5 5 1 1
1 2
2 3
3 4
2 5
3 5
Sample Output 3
0
Comments