import heapq

N, M = list(map(int, input().split()))

Edge = [list(map(int, input().split())) for i in range(M)]



S, T = list(map(int, input().split()))

S, T = S - 1, T - 1





G3 = [[] for i in range(3 * N)]

for u, v in Edge:

    u, v = u - 1, v - 1

    G3[u].append([v + N, 1])

    G3[u + N].append([v + N * 2, 1])

    G3[u + 2 * N].append([v, 1])





def dijkstra(x):

    d = [float('inf')] * (3 * N)

    d[x] = 0

    visited = {x}



    # d, u

    hq = [(0, x)]



    while hq:

        u = heapq.heappop(hq)[1]

        visited.add(u)



        for node, cost in G3[u]:

            if (node not in visited) and d[node] > d[u] + cost:

                d[node] = d[u] + cost

                heapq.heappush(hq, (d[u] + cost, node))

    return d





dist = dijkstra(S)

print((dist[T] // 3 if dist[T] != float('inf') else - 1))

N, M = list(map(int, input().split()))

G = [[] for i in range(3 * N)]

for i in range(M):

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

    u, v = u - 1, v - 1

    G[u].append([v + N, 1])

    G[u + N].append([v + 2 * N, 1])

    G[u + 2 * N].append([v, 1])



S, T = list(map(int, input().split()))

S, T = S - 1, T - 1





def dijkstra(graph, start, inf=float('inf')):

    import heapq

    n = len(graph)

    distances = [inf] * n

    distances[start] = 0

    visited = [False] * n



    # 距離・頂点

    hq = [(0, start)]

    while hq:

        dist, fr = heapq.heappop(hq)

        visited[fr] = True



        for to, cost in graph[fr]:

            new_dist = distances[fr] + cost

            if (visited[to]) or (distances[to] <= new_dist):

                continue



            distances[to] = new_dist

            heapq.heappush(hq, (new_dist, to))



    return distances





distances = dijkstra(G, S)

print((distances[T] // 3 if distances[T] != float('inf') else -1))

import heapq

N, M = list(map(int, input().split()))
Edge = [list(map(int, input().split())) for i in range(M)]
S, T = list(map(int, input().split()))
S, T = S - 1, T - 1
G3 = [[] for i in range(3 * N)]
for u, v in Edge:
    u, v = u - 1, v - 1
    G3[u].append([v + N, 1])
    G3[u + N].append([v + N * 2, 1])
    G3[u + 2 * N].append([v, 1])


def dijkstra(x):
    d = [float("inf")] * (3 * N)
    d[x] = 0
    visited = {x}
    # d, u
    hq = [(0, x)]
    while hq:
        u = heapq.heappop(hq)[1]
        visited.add(u)
        for node, cost in G3[u]:
            if (node not in visited) and d[node] > d[u] + cost:
                d[node] = d[u] + cost
                heapq.heappush(hq, (d[u] + cost, node))
    return d


dist = dijkstra(S)
print((dist[T] // 3 if dist[T] != float("inf") else -1))

N, M = list(map(int, input().split()))
G = [[] for i in range(3 * N)]
for i in range(M):
    u, v = list(map(int, input().split()))
    u, v = u - 1, v - 1
    G[u].append([v + N, 1])
    G[u + N].append([v + 2 * N, 1])
    G[u + 2 * N].append([v, 1])
S, T = list(map(int, input().split()))
S, T = S - 1, T - 1


def dijkstra(graph, start, inf=float("inf")):
    import heapq

    n = len(graph)
    distances = [inf] * n
    distances[start] = 0
    visited = [False] * n
    # 距離・頂点
    hq = [(0, start)]
    while hq:
        dist, fr = heapq.heappop(hq)
        visited[fr] = True
        for to, cost in graph[fr]:
            new_dist = distances[fr] + cost
            if (visited[to]) or (distances[to] <= new_dist):
                continue
            distances[to] = new_dist
            heapq.heappush(hq, (new_dist, to))
    return distances


distances = dijkstra(G, S)
print((distances[T] // 3 if distances[T] != float("inf") else -1))

-import heapq - -Edge = [list(map(int, input().split())) for i in range(M)] +G = [[] for i in range(3 * N)] +for i in range(M): +    u, v = list(map(int, input().split())) +    u, v = u - 1, v - 1 +    G[u].append([v + N, 1]) +    G[u + N].append([v + 2 * N, 1]) +    G[u + 2 * N].append([v, 1]) -G3 = [[] for i in range(3 * N)] -for u, v in Edge: -    u, v = u - 1, v - 1 -    G3[u].append([v + N, 1]) -    G3[u + N].append([v + N * 2, 1]) -    G3[u + 2 * N].append([v, 1]) -def dijkstra(x): -    d = [float("inf")] * (3 * N) -    d[x] = 0 -    visited = {x} -    # d, u -    hq = [(0, x)] +def dijkstra(graph, start, inf=float("inf")): +    import heapq + +    n = len(graph) +    distances = [inf] * n +    distances[start] = 0 +    visited = [False] * n +    # 距離・頂点 +    hq = [(0, start)] -        u = heapq.heappop(hq)[1] -        visited.add(u) -        for node, cost in G3[u]: -            if (node not in visited) and d[node] > d[u] + cost: -                d[node] = d[u] + cost -                heapq.heappush(hq, (d[u] + cost, node)) -    return d +        dist, fr = heapq.heappop(hq) +        visited[fr] = True +        for to, cost in graph[fr]: +            new_dist = distances[fr] + cost +            if (visited[to]) or (distances[to] <= new_dist): +                continue +            distances[to] = new_dist +            heapq.heappush(hq, (new_dist, to)) +    return distances -dist = dijkstra(S) -print((dist[T] // 3 if dist[T] != float("inf") else -1)) +distances = dijkstra(G, S) +print((distances[T] // 3 if distances[T] != float("inf") else -1))