Heroes in Indian movies are capable of superhuman feats. For example, they can jump between buildings, jump onto and from running trains, catch bullets with their hands and teeth and so on. A perceptive follower of such movies would have noticed that there are limits to what even the superheroes can do. For example, if the hero could directly jump to his ultimate destination, that would reduce the action sequence to nothing and thus make the movie quite boring. So he typically labours through a series of superhuman steps to reach his ultimate destination.
In this problem, our hero has to save his wife/mother/child/dog/… held captive by the nasty villain on the top floor of a tall building in the centre of Bombay/Bangkok/Kuala Lumpur/…. Our hero is on top of a (different) building. In order to make the action "interesting" the director has decided that the hero can only jump between buildings that are "close" to each other. The director decides which pairs of buildings are close enough and which are not.
Given the list of buildings, the identity of the building where the hero begins his search, the identity of the building where the captive (wife/mother/child/dog…) is held, and the set of pairs of buildings that the hero can jump across, your aim is determine whether it is possible for the hero to reach the captive. And, if he can reach the captive he would like to do so with minimum number of jumps.
Here is an example. There are $5$ buildings, numbered $1,2,...,5$, the hero stands on building $1$ and the captive is on building $4$. The director has decided that buildings $1$ and $3$, $2$ and $3, 1$ and $2, 3$ and $5$ and $4$ and $5$ are close enough for the hero to jump across. The hero can save the captive by jumping from $1$ to $3$ and then from $3$ to $5$ and finally from $5$ to $4$. (Note that if $i$ and $j$ are close then the hero can jump from $i$ to $j$ as well as from $j$ to $i$.). In this example, the hero could have also reached $4$ by jumping from $1$ to $2, 2$ to $3, 3$ to $5$ and finally from $5$ to $4$. The first route uses $3$ jumps while the second one uses $4$ jumps. You can verify that $3$ jumps is the best possible.
If the director decides that the only pairs of buildings that are close enough are $1$ and $3$, $1$ and $2$ and $4$ and $5$, then the hero would not be able to reach building $4$ to save the captive.

-----Input:-----
The first line of the input contains two integers $N$ and $M$. $N$ is the number of buildings: we assume that our buildings are numbered $1,2,...,N$. $M$ is the number of pairs of buildings that the director lists as being close enough to jump from one to the other. Each of the next $M$ lines, lines $2,...,M+1$, contains a pair of integers representing a pair of buildings that are close. Line $i+1$ contains integers $A_i$ and $B_i$, $1 \leq A_i \leq N$ and $1 \leq B_i \leq N$, indicating that buildings $A_i$ and $B_i$ are close enough. The last line, line $M+2$ contains a pair of integers $S$ and $T$, where $S$ is the building from which the Hero starts his search and $T$ is the building where the captive is held.

-----Output:-----
If the hero cannot reach the captive print $0$. If the hero can reach the captive print out a single integer indicating the number of jumps in the shortest route (in terms of the number of jumps) to reach the captive.

-----Constraints:-----
- $1 \leq N \leq 3500$.
- $1 \leq M \leq 1000000$.
- In at least $50 \%$ of the inputs $1 \leq N \leq 1000$ and $1 \leq M \leq 200000$.

-----Sample Input 1:-----
5 5
1 3
2 3
1 2
3 5
4 5 
1 4

-----Sample Output 1:-----
3 

-----Sample Input 2:-----
5 3
1 3
1 2
4 5
1 4

-----Sample Output 2:-----
0

Source: codechef | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
from sys import stdin, stdout
from collections import deque
st = lambda : list(stdin.readline().strip())
li = lambda : list(map(int, stdin.readline().split()))
mp = lambda : list(map(int, stdin.readline().split()))
inp = lambda : int(stdin.readline())
pr = lambda n: stdout.write(str(n) + '\n')
mod = 1000000007
INF = float('inf')

def solve():
	(n, m) = mp()
	d = {i: [] for i in range(n + 1)}
	for i in range(m):
		(a, b) = mp()
		d[a].append(b)
		d[b].append(a)
	(x, y) = mp()
	v = [False for i in range(n + 1)]
	q = deque()
	q.append(x)
	dis = [INF for i in range(n + 1)]
	dis[x] = 0
	v[x] = True
	while q:
		a = q.popleft()
		for i in d[a]:
			if not v[i]:
				v[i] = True
				q.append(i)
				dis[i] = dis[a] + 1
	if not v[y]:
		pr(0)
	else:
		pr(dis[y])
for _ in range(1):
	solve()


(n, m) = map(int, input().strip().split())
connections = {}
for _ in range(m):
	(x, y) = map(int, input().strip().split())
	if x not in connections.keys():
		connections[x] = [y]
	else:
		connections[x].append(y)
	if y not in connections.keys():
		connections[y] = [x]
	else:
		connections[y].append(x)
(s, t) = map(int, input().split())
distances = [-1] * (n + 1)
distances[s] = 0
unvisited = list(range(1, n + 1))
while unvisited != []:
	min1 = max(distances)
	v = -1
	flag = 0
	for j in unvisited:
		if distances[j] != -1 and min1 >= distances[j]:
			min1 = distances[j]
			v = j
	if v == -1:
		break
	unvisited.remove(v)
	for i in connections[v]:
		if i not in unvisited:
			continue
		if distances[i] == -1 or distances[i] > 1 + distances[v]:
			distances[i] = 1 + distances[v]
		if i == t:
			flag = 1
			break
	if flag == 1:
		break
if distances[t] == -1:
	distances[t] = 0
print(distances[t])


from queue import Queue
(n, m) = [int(x) for x in input().split()]
adj = [[] for i in range(n + 1)]
for _ in range(m):
	(src, dest) = [int(x) for x in input().split()]
	adj[src].append(dest)
	adj[dest].append(src)
vis = [0] * (n + 1)
(strt, end) = [int(x) for x in input().split()]
vis[strt] = 1
q = Queue()
q.put(strt)
k = 0
while not q.empty():
	k += 1
	for i in range(q.qsize()):
		ele = q.get()
		for j in adj[ele]:
			if vis[j] == 0:
				q.put(j)
				vis[j] = k
print(vis[end])


from collections import deque

def bfs(graph, u, t):
	queue = deque([u])
	level = {u: 0}
	while queue:
		ver = queue.popleft()
		for v in graph[ver]:
			if v not in level:
				queue.append(v)
				level[v] = level[ver] + 1
	return level.get(t, 0)
try:
	(n, e) = map(int, input().split())
	graph = {}
	for i in range(1, n + 1):
		graph[i] = []
	for _ in range(e):
		(a, b) = map(int, input().split())
		graph[a].append(b)
		graph[b].append(a)
	(s, t) = map(int, input().split())
	p = bfs(graph, s, t)
	print(p)
except:
	pass