from collections import deque

n, m = list(map(int, input().split()))

inf = 10**9

def bfs(start):

    dist = [inf for i in range((n+1) * 3)]

    que = deque()

    que.append(start)

    finished = set()

    dist[start] = 0

    while que:

        node = que.popleft()

        if node not in finished:

            finished.add(node)

            for i in adjacent_list[node]:

                dist[i] = min(dist[i], dist[node] + 1)

                que.append(i)

    return dist



adjacent_list = [[] for i in range((n+1)*3)]

for _ in range(m):

    u, v = list(map(int, input().split()))

    for i in range(3):

        adjacent_list[u*3 + i].append(v*3 +((i+1)%3))

start, goal = list(map(int, input().split()))

graph = bfs(start*3)

if graph[goal*3] != inf:

    print((int(graph[goal*3]) // 3))

else:

    print((-1))
from collections import deque

n, m = list(map(int, input().split()))

adjacent_list = [[] for i in range(3*n + 3)]

for i in range(m):

    u, v = list(map(int, input().split()))

    for j in range(1,4):

        p = j + 3 * u - 3

        q = (j % 3) + 3 * v - 2

        #print("{} -> {}".format(p, q))

        adjacent_list[p].append(q)

s, t = list(map(int, input().split()))



def bfs(start, target):

    que = deque()

    finished = set()

    que.append([start, 0])

    while que:

        node, dist = que.popleft()

        if (node, dist % 3) in finished:

            continue

        if node == target:

            return dist // 3

        for i in adjacent_list[node]:

            que.append([i, dist+1])

            finished.add((node, dist % 3))

    return -1

print((bfs(s*3 -2, 3*t-2)))



from collections import deque

n, m = list(map(int, input().split()))
inf = 10**9


def bfs(start):
    dist = [inf for i in range((n + 1) * 3)]
    que = deque()
    que.append(start)
    finished = set()
    dist[start] = 0
    while que:
        node = que.popleft()
        if node not in finished:
            finished.add(node)
            for i in adjacent_list[node]:
                dist[i] = min(dist[i], dist[node] + 1)
                que.append(i)
    return dist


adjacent_list = [[] for i in range((n + 1) * 3)]
for _ in range(m):
    u, v = list(map(int, input().split()))
    for i in range(3):
        adjacent_list[u * 3 + i].append(v * 3 + ((i + 1) % 3))
start, goal = list(map(int, input().split()))
graph = bfs(start * 3)
if graph[goal * 3] != inf:
    print((int(graph[goal * 3]) // 3))
else:
    print((-1))

from collections import deque

n, m = list(map(int, input().split()))
adjacent_list = [[] for i in range(3 * n + 3)]
for i in range(m):
    u, v = list(map(int, input().split()))
    for j in range(1, 4):
        p = j + 3 * u - 3
        q = (j % 3) + 3 * v - 2
        # print("{} -> {}".format(p, q))
        adjacent_list[p].append(q)
s, t = list(map(int, input().split()))


def bfs(start, target):
    que = deque()
    finished = set()
    que.append([start, 0])
    while que:
        node, dist = que.popleft()
        if (node, dist % 3) in finished:
            continue
        if node == target:
            return dist // 3
        for i in adjacent_list[node]:
            que.append([i, dist + 1])
            finished.add((node, dist % 3))
    return -1


print((bfs(s * 3 - 2, 3 * t - 2)))

-inf = 10**9 +adjacent_list = [[] for i in range(3 * n + 3)] +for i in range(m): +    u, v = list(map(int, input().split())) +    for j in range(1, 4): +        p = j + 3 * u - 3 +        q = (j % 3) + 3 * v - 2 +        # print("{} -> {}".format(p, q)) +        adjacent_list[p].append(q) +s, t = list(map(int, input().split())) -def bfs(start): -    dist = [inf for i in range((n + 1) * 3)] +def bfs(start, target): -    que.append(start) -    dist[start] = 0 +    que.append([start, 0]) -        node = que.popleft() -        if node not in finished: -            finished.add(node) -            for i in adjacent_list[node]: -                dist[i] = min(dist[i], dist[node] + 1) -                que.append(i) -    return dist +        node, dist = que.popleft() +        if (node, dist % 3) in finished: +            continue +        if node == target: +            return dist // 3 +        for i in adjacent_list[node]: +            que.append([i, dist + 1]) +            finished.add((node, dist % 3)) +    return -1 -adjacent_list = [[] for i in range((n + 1) * 3)] -for _ in range(m): -    u, v = list(map(int, input().split())) -    for i in range(3): -        adjacent_list[u * 3 + i].append(v * 3 + ((i + 1) % 3)) -start, goal = list(map(int, input().split())) -graph = bfs(start * 3) -if graph[goal * 3] != inf: -    print((int(graph[goal * 3]) // 3)) -else: -    print((-1)) +print((bfs(s * 3 - 2, 3 * t - 2)))