Statement
I’m feeling sick, I’m feeling sick!
Minas Gerais is a state with many hills (or, as many people say, a sea of hills). Fernanda wants to travel through Minas Gerais, but she always has problems with altitude and may feel sick from the lack of oxygen. The state can be described by cities connected by roads that can be traveled in both directions. City has altitude meters. Fernanda starts in city 1 and wants to reach city .
Acclimatization works as follows: the body adapts to an altitude after sleeping in a city. More specifically, after sleeping in city , whose altitude is , on the following day Fernanda may visit only cities whose altitudes lie in , where is fixed. She may visit several cities on the same day.
Under this restriction, find the minimum number of days Fernanda needs to reach city , or print if it is impossible.
Input
The first line contains three integers (, , ). The second line contains integers; the -th is (). The next lines describe the roads. The -th contains (, ), representing a road between those cities.
No pair of cities is connected more than once, and there are no self-loops.
Output
Print the minimum number of days required to leave city 1 and reach city , or if it is impossible.
Examples
Input
8 10 4
1 6 6 7 3 3 5 8
1 4
1 6
2 5
6 4
4 2
4 5
7 5
1 8
5 8
7 8
Output
3
Fernanda can make the following moves on each day:
- ;
- ;
- .
Input
2 1 1
1 1
1 2
Output
1
Here Fernanda can go directly on the first day.
Input
3 2 3
1 2 8
1 2
2 3
Output
-1
Tutorial
Let be the minimum number of days needed to reach when waking in , with . To compute it, activate exactly the vertices whose altitudes lie in ; the next sleeping city may be anywhere in ‘s active connected component, so .
Process vertices in decreasing altitude. The active window changes by two pointers, producing only linearly many edge insertions and deletions. Generate these operations offline and answer component-minimum queries with a rollback DSU over a segment tree of time.
When becomes known, attach a fresh auxiliary vertex carrying that value to ; this turns a value update into an insertion and preserves component minima. Complexity is . A more involved solution achieves .