#K69837. Treasure Hunt

    ID: 33175 Type: Default 1000ms 256MiB

Treasure Hunt

Treasure Hunt

You are given a series of clues that form a directed graph. Each clue is represented by a pair of integers (a, b) which indicates that clue a leads to clue b. The treasure is located at the clue numbered 0. Given a starting clue, your task is to determine the minimum number of steps required to reach the treasure by following the clues. If the treasure cannot be reached, output -1.

The input consists of multiple datasets. For each dataset, the first line contains an integer n representing the number of clues. If n is 0, it marks the end of the input. The next n lines each contain two space-separated integers representing the clues. The line following these clues contains the starting clue.

inputFormat

The input is read from standard input (stdin). It consists of multiple datasets. Each dataset begins with an integer n (number of clues). If n equals 0, the input ends. Then follow n lines, each containing two integers a and b (representing a directed clue from a to b). Finally, a line containing the starting clue is provided.

outputFormat

For each dataset, output a single integer on a new line to standard output (stdout). This integer is the minimum number of steps required to reach the treasure (clue 0) from the starting clue. If the treasure is unreachable, output -1.## sample

5
1 2
2 3
3 4
4 0
5 1
1
3
1 3
3 4
4 3
1
0
4

-1

</p>