Bamboo Forest of the Lost - MarisaOJ: Marisa Online Judge

Bamboo Forest of the Lost

Time limit: 1000 ms
Memory limit: 256 MB
Marisa and Reimu have lost each other in the Bamboo Forest of the Lost. The forest can be represented as a undirected graph of n vertices and m edges. Marisa is currently at vertex a, and Reimu is currently at vertex b. Every 1 unit of time, Marisa and Reimu move to any adjacent vertex (they will not stand still, as they are in a hurry now). Find the earliest time when they will meet each other again. ### Input - The first line contains 2 integers n,m. - The second line contains 2 integers a,b. - The next m lines, each line contains 2 integers u,v, there is an edge between u and v. ### Output - Print the earliest time when they will meet each other again. Or print -1 if they get lost forever... ### Constraints - 1≤n,m≤105. - 1≤u,v,a,b≤n. ### Example Input: 6616122334246554 Output: 2 #### **Note**: Marisa and Reimu will meet each other at 4.