Problem C
Haunted Graveyard
Tonight is Halloween and Scared John and his friends have decided to do something fun to celebrate the occasion: crossing the graveyard. Although Scared John does not find this fun at all, he finally agreed to join them in their adventure. Once at the entrance, the friends have begun to cross the graveyard one by one, and now it is the time for Scared John. He still remembers the tales his grandmother told him when he was a child. She told him that, on Halloween night, “haunted holes” appear in the graveyard. These are not usual holes, but they transport people who fall inside to some point in the graveyard, possibly far away. But the scariest feature of these holes is that they allow one to travel in time as well as in space; i.e., if you fall inside a “haunted hole”, you appear somewhere in the graveyard a certain time before (or after) you entered the hole, in a parallel universe otherwise identical to ours.
The graveyard is organized as a grid of
-
If the cell contains a gravestone, you cannot walk over it, because gravestones are too high to climb.
-
If the cell contains a “haunted hole” and you walk over it, you will appear somewhere in the graveyard at a possibly different moment in time. The time difference depends on the particular “haunted hole” you fell into, and can be positive, negative or zero.
-
Otherwise, the cell has only grass, and you can walk freely over it.
He is terrified, so he wants to cross the graveyard as quickly as possible. And that is the reason why he has phoned you, a renowned programmer. He wants you to write a program that, given the description of the graveyard, computes the minimum time needed to go from the entrance to the exit. Scared John accepts using “haunted holes” if they permit him to cross the graveyard quicker, but he is frightened to death of the possibility of getting lost and being able to travel back in time indefinitely using the holes, so your program must report these situations.

Figure 3 illustrates a possible graveyard (the second
test case from the sample input). In this case there are two
gravestones in cells
If you do not use the “haunted hole”, you need at least
Note that the destination of a “haunted hole” may have the entrance to another “haunted hole”. In this situation, Scared John will enter the second hole as soon as he exits the first one.
Scared John will leave the graveyard as soon as he reaches the exit, even if he would be able to travel back in time by continuing walking through the graveyard.
Input
The input consists of several test cases (at most
The next line contains an integer
You can safely assume that there are no two “haunted holes”
with the same origin, and that neither the entrance nor the
destination of a “haunted hole” contains a gravestone.
Furthermore, there are neither gravestones nor “haunted holes”
at positions
The input will finish with a line containing 0 0, which should not be processed.
Output
For each test case, if it is possible for Scared John to travel back in time indefinitely without passing by the exit, output Never. Otherwise, print the minimum time in seconds that it takes him to cross the graveyard from the entrance to the exit if it is reachable, and Impossible if not.
Sample Input 1 | Sample Output 1 |
---|---|
3 3 2 2 1 1 2 0 4 3 2 2 1 3 1 1 3 0 2 2 0 4 2 0 1 2 0 1 0 -3 0 0 |
Impossible 4 Never |