Celebration
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Today everyone is celebrating Happy Numbers Day!
Zenyk and Marichka are celebrating it in Lviv. They have already visited 47 bars, but Marichka started insisting that it is time to go home. Zenyk wants to continue the celebration and therefore decides to run away from Marichka.
Since neither Zenyk nor Marichka used to walk around the bars of Lviv very often, they do not know all possible roads. Only those that they once read about in tourist guides. Moreover, the roads Zenyk knows are not necessarily known by Marichka and vice versa. Considering that tourist guides often do not tell about all the roads, but only about a limited number of them, both Zenyk and Marichka know at most one path between each pair of bars. That is, the roads known by Zenyk form a set of one or more trees. Also, the roads known by Marichka form a set of one or more trees.
Earlier, Zenyk and Marichka installed an app that allows them to view each other’s location at any given time. Now, once an hour, Zenyk checks where Marichka is and decides whether to stay in his current bar or move to a neighboring one via one of the roads known to him. Marichka understands how Zenyk acts, so after Zenyk has already chosen a new bar, she chooses whether to go to a neighboring bar via one of the roads known to Marichka, or stay in her current one. Since Marichka is very smart, she will choose bars in such a way as to meet Zenyk as soon as possible, considering that Zenyk will also choose bars in such a way as to celebrate the biggest holiday of the year for as long as possible.
However, Zenyk is not as smart as Marichka, so he needs your help. Tell how many hours Zenyk will be able to celebrate before Marichka finds him. If the celebration can last forever, output \(\texttt{-1}\).
Input
The first line contains 5 integers: the number of bars in Lviv \(n\), the number of roads Zenyk knows \(m_1\), the number of roads Marichka knows \(m_2\), the bar where Zenyk is located \(a\), and the bar where Marichka is located \(b\).
The next \(m_1\) lines contain a pair of numbers \(u\), \(v\) – they indicate that Zenyk knows a road between bars \(u\) and \(v\).
The next \(m_2\) lines contain a pair of numbers \(u\), \(v\) – they indicate that Marichka knows a road between bars \(u\) and \(v\).
Output
In a single line, output a single integer – the number of hours after which Marichka will find Zenyk, or \(\texttt{-1}\) if Zenyk will celebrate Happy Numbers Day until old age.
Constraints
\(1 \le n \le 10^{5}\),
\(0 \le m_1, m_2 \le n - 1\),
the roads known by Zenyk form a set of one or more trees,
the roads known by Marichka form a set of one or more trees,
\(1 \le a, b \le n\),
\(a \neq b\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 5 4 4 1 4 1 2 2 3 3 4 3 5 1 2 2 3 2 4 4 5 | 2 |
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|