from collections import deque

def bfs(start, goal, g, n, visited):

    visited1 = [False]*(n+1)

    visited2 = [False]*(n+1)

    q = deque([start])

    visited[start] = 0

    while q:

        curr_node = q.popleft()

        if curr_node == goal: 

            return

        for next1_node in g[curr_node]:

            if visited1[next1_node]: continue

            visited1[next1_node] = True

            for next2_node in g[next1_node]:

                if visited2[next2_node]: continue

                visited2[next2_node] = True

                for next3_node in g[next2_node]:

                    if visited[next3_node] >= 0: continue

                    visited[next3_node] = visited[curr_node] + 1

                    q.append(next3_node)







def main():

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

    g = [ [] for _ in range(n+1)]



    for _ in range(m):

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

        g[u].append(v)



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

    



    visited = [-1]*(n+1)

    visited[0] = 0



    bfs(s, t, g, n, visited)

    print((visited[t]))



if __name__ == "__main__":

    main()
from collections import deque

def bfs(start, g, visited, goal):

    q = deque([start])

    visited[start] = 0

    while q:

        curr_node = q.popleft()

        for next_node in g[curr_node]:

            if visited[next_node] >= 0: continue

            visited[next_node] = visited[curr_node] + 1

            if next_node == goal: return

            q.append(next_node)





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

g = [ [] for _ in range(3*n)]



for _ in range(m):

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

    g[u*3-3].append(v*3-2)

    g[u*3-2].append(v*3-1)

    g[u*3-1].append(v*3-3)



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

visited = [-3]*(3*n)

bfs(s*3-3, g, visited, t*3-3)

print((visited[t*3-3]//3))
from collections import deque


def bfs(start, goal, g, n, visited):
    visited1 = [False] * (n + 1)
    visited2 = [False] * (n + 1)
    q = deque([start])
    visited[start] = 0
    while q:
        curr_node = q.popleft()
        if curr_node == goal:
            return
        for next1_node in g[curr_node]:
            if visited1[next1_node]:
                continue
            visited1[next1_node] = True
            for next2_node in g[next1_node]:
                if visited2[next2_node]:
                    continue
                visited2[next2_node] = True
                for next3_node in g[next2_node]:
                    if visited[next3_node] >= 0:
                        continue
                    visited[next3_node] = visited[curr_node] + 1
                    q.append(next3_node)


def main():
    n, m = list(map(int, input().split()))
    g = [[] for _ in range(n + 1)]
    for _ in range(m):
        u, v = list(map(int, input().split()))
        g[u].append(v)
    s, t = list(map(int, input().split()))
    visited = [-1] * (n + 1)
    visited[0] = 0
    bfs(s, t, g, n, visited)
    print((visited[t]))


if __name__ == "__main__":
    main()

from collections import deque


def bfs(start, g, visited, goal):
    q = deque([start])
    visited[start] = 0
    while q:
        curr_node = q.popleft()
        for next_node in g[curr_node]:
            if visited[next_node] >= 0:
                continue
            visited[next_node] = visited[curr_node] + 1
            if next_node == goal:
                return
            q.append(next_node)


n, m = list(map(int, input().split()))
g = [[] for _ in range(3 * n)]
for _ in range(m):
    u, v = list(map(int, input().split()))
    g[u * 3 - 3].append(v * 3 - 2)
    g[u * 3 - 2].append(v * 3 - 1)
    g[u * 3 - 1].append(v * 3 - 3)
s, t = list(map(int, input().split()))
visited = [-3] * (3 * n)
bfs(s * 3 - 3, g, visited, t * 3 - 3)
print((visited[t * 3 - 3] // 3))

-def bfs(start, goal, g, n, visited): -    visited1 = [False] * (n + 1) -    visited2 = [False] * (n + 1) +def bfs(start, g, visited, goal): -        if curr_node == goal: -            return -        for next1_node in g[curr_node]: -            if visited1[next1_node]: +        for next_node in g[curr_node]: +            if visited[next_node] >= 0: -            visited1[next1_node] = True -            for next2_node in g[next1_node]: -                if visited2[next2_node]: -                    continue -                visited2[next2_node] = True -                for next3_node in g[next2_node]: -                    if visited[next3_node] >= 0: -                        continue -                    visited[next3_node] = visited[curr_node] + 1 -                    q.append(next3_node) +            visited[next_node] = visited[curr_node] + 1 +            if next_node == goal: +                return +            q.append(next_node) -def main(): -    n, m = list(map(int, input().split())) -    g = [[] for _ in range(n + 1)] -    for _ in range(m): -        u, v = list(map(int, input().split())) -        g[u].append(v) -    s, t = list(map(int, input().split())) -    visited = [-1] * (n + 1) -    visited[0] = 0 -    bfs(s, t, g, n, visited) -    print((visited[t])) - - -if __name__ == "__main__": -    main() +n, m = list(map(int, input().split())) +g = [[] for _ in range(3 * n)] +for _ in range(m): +    u, v = list(map(int, input().split())) +    g[u * 3 - 3].append(v * 3 - 2) +    g[u * 3 - 2].append(v * 3 - 1) +    g[u * 3 - 1].append(v * 3 - 3) +s, t = list(map(int, input().split())) +visited = [-3] * (3 * n) +bfs(s * 3 - 3, g, visited, t * 3 - 3) +print((visited[t * 3 - 3] // 3))