In recent years space travel has become significantly cheaper. With this advancement in technology, space transit has become a booming business, and you just got hired as the logistics coordinator!
You have been provided with a list of nearby planets, each with a unique integer id \(a_i\space(a_i>0)\).
Each planet is also supplemented with a list of all \(M\space(0\le M\le N)\) planets, with unique integer ids \(m_i\space(m_i>0)\), that a spaceship could reach with one tank of gas.
You have been tasked with writing a program to find the minimum number of stops that a spaceship needs to make in order to transport a client from their starting planet
to their destination planet
.
If the trip is not possible, output -1
Input Specification
The first line of input contains two space separated integers, and
.
The next line of input contains the number of nearby planets .
The following lines contain an integer
, followed by an integer
, followed by
integers
.
Output Specification
Output one integer , the minimum number of stops required, or
-1
.
Constraints
Subtask 1 [40%]
Subtask 2 [60%]
Sample Input
1 4
4
1 1 2
2 3 1 3 4
3 2 2 4
4 2 2 3
Sample Output
1
Sample Explanation
In this case, the spaceship starts at planet 1
and refuels at planet 2
in order to make it to planet 4
, for a total of 1 stop.
Comments