Duck Duck Patapim
You are playing Duck Duck Patapim in a park with benches connected by
walkways.
You start at bench and want to shout "GO!" while standing at bench
. Each walkway connects two benches in both directions, and crossing one walkway counts as one move.
Some benches are special. You may visit benches and cross walkways multiple times. A route is valid only if, when you shout "GO!", both of the following are true:
- You are currently at bench
.
- At some point along your route, including the starting and ending benches, you have visited at least one special bench.
For example, if but bench
is not special, the answer is not
: staying still does not visit a special bench. You must move away, visit a special bench, and then return to
.
What is the minimum number of moves needed? If it is impossible, print .
Input
The first line contains two integers and
: the number of benches and walkways.
The second line contains integers
, where each
is
or
. If
, bench
is a special bench; otherwise it is a normal bench.
Each of the next lines contains two integers
and
, meaning there is a walkway between benches
and
. No walkway appears more than once.
The last line contains two integers and
: your starting bench and the bench where you want to shout "GO!".
Output
Print one integer: the minimum number of moves in a valid route, or if no valid route exists.
Example
Input 1
3 2
0 1 0
1 2
2 3
1 1
Output 1
2
You start and finish at bench , but bench
is not special. A shortest valid route is
: you visit the special bench
, then return to bench
. Simply staying at bench
is invalid, even though
.
Input 2
5 4
0 1 0 0 0
1 2
2 3
3 4
4 5
1 5
Output 2
4
A shortest valid route is . You visit the special bench
and finish at bench
after four moves.
Input 3
4 2
0 0 1 0
1 2
3 4
1 4
Output 3
-1
Benches and
are disconnected from benches
and
. Since you start at bench
, you can never reach the component containing the only special bench and
.
Comments