import heapq

import os

import sys



if os.getenv("LOCAL"):

    sys.stdin = open("_in.txt", "r")



sys.setrecursionlimit(2147483647)

INF = float("inf")

IINF = 10 ** 18



N, M = list(map(int, sys.stdin.readline().split()))

UV = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]

S, T = list(map(int, sys.stdin.readline().split()))



U = []

V = []

for u, v in UV:

    U.extend([u, u + N, u + N * 2])

    V.extend([v + N, v + N * 2, v])



# graph = csr_matrix(([1] * (M * 3), (U, V)), shape=(N * 3 + 1, N * 3 + 1))

# dist = dijkstra(graph, indices=S)[T]

# if dist < np.inf:

#     print(int(dist // 3))

# else:

#     print(-1)



#: :type: list of (list of int)

graph = [[] for _ in range(N * 3 + 1)]

for u, v in zip(U, V):

    graph[u].append(v)



# S から T までの距離

heap = [(0, S)]

dist = [IINF] * (N * 3 + 1)

while heap:

    d, v = heapq.heappop(heap)

    if v == T:

        print((int(d // 3)))

        exit()

    for u in graph[v]:

        if d + 1 < dist[u]:

            dist[u] = d + 1

            heapq.heappush(heap, (d + 1, u))

print((-1))

import heapq

import os

import sys



if os.getenv("LOCAL"):

    sys.stdin = open("_in.txt", "r")



sys.setrecursionlimit(2147483647)

INF = float("inf")

IINF = 10 ** 18



N, M = list(map(int, sys.stdin.readline().split()))

VU = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]

S, T = list(map(int, sys.stdin.readline().split()))



#: :type: list of (list of int)

graph = [[] for _ in range(N * 3 + 1)]

for v, u in VU:

    graph[v].append(u + N)

    graph[v + N].append(u + N * 2)

    graph[v + N * 2].append(u)



heap = [(0, S)]

dist = [INF] * (N * 3 + 1)

ans = -1

while heap:

    d, v = heapq.heappop(heap)

    if v == T and d % 3 == 0:

        ans = int(d // 3)

        break

    for u in graph[v]:

        if d + 1 < dist[u]:

            dist[u] = d + 1

            heapq.heappush(heap, (d + 1, u))

print(ans)

import heapq
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")
sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10**18
N, M = list(map(int, sys.stdin.readline().split()))
UV = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]
S, T = list(map(int, sys.stdin.readline().split()))
U = []
V = []
for u, v in UV:
    U.extend([u, u + N, u + N * 2])
    V.extend([v + N, v + N * 2, v])
# graph = csr_matrix(([1] * (M * 3), (U, V)), shape=(N * 3 + 1, N * 3 + 1))
# dist = dijkstra(graph, indices=S)[T]
# if dist < np.inf:
#     print(int(dist // 3))
# else:
#     print(-1)
#: :type: list of (list of int)
graph = [[] for _ in range(N * 3 + 1)]
for u, v in zip(U, V):
    graph[u].append(v)
# S から T までの距離
heap = [(0, S)]
dist = [IINF] * (N * 3 + 1)
while heap:
    d, v = heapq.heappop(heap)
    if v == T:
        print((int(d // 3)))
        exit()
    for u in graph[v]:
        if d + 1 < dist[u]:
            dist[u] = d + 1
            heapq.heappush(heap, (d + 1, u))
print((-1))

import heapq
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")
sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10**18
N, M = list(map(int, sys.stdin.readline().split()))
VU = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]
S, T = list(map(int, sys.stdin.readline().split()))
#: :type: list of (list of int)
graph = [[] for _ in range(N * 3 + 1)]
for v, u in VU:
    graph[v].append(u + N)
    graph[v + N].append(u + N * 2)
    graph[v + N * 2].append(u)
heap = [(0, S)]
dist = [INF] * (N * 3 + 1)
ans = -1
while heap:
    d, v = heapq.heappop(heap)
    if v == T and d % 3 == 0:
        ans = int(d // 3)
        break
    for u in graph[v]:
        if d + 1 < dist[u]:
            dist[u] = d + 1
            heapq.heappush(heap, (d + 1, u))
print(ans)

-UV = [list(map(int, sys.stdin.readline().split())) for _ in range(M)] +VU = [list(map(int, sys.stdin.readline().split())) for _ in range(M)] -U = [] -V = [] -for u, v in UV: -    U.extend([u, u + N, u + N * 2]) -    V.extend([v + N, v + N * 2, v]) -# graph = csr_matrix(([1] * (M * 3), (U, V)), shape=(N * 3 + 1, N * 3 + 1)) -# dist = dijkstra(graph, indices=S)[T] -# if dist < np.inf: -#     print(int(dist // 3)) -# else: -#     print(-1) -for u, v in zip(U, V): -    graph[u].append(v) -# S から T までの距離 +for v, u in VU: +    graph[v].append(u + N) +    graph[v + N].append(u + N * 2) +    graph[v + N * 2].append(u) -dist = [IINF] * (N * 3 + 1) +dist = [INF] * (N * 3 + 1) +ans = -1 -    if v == T: -        print((int(d // 3))) -        exit() +    if v == T and d % 3 == 0: +        ans = int(d // 3) +        break -print((-1)) +print(ans)