import sys

from collections import deque

sys.setrecursionlimit(10**6)



def main():

    input = sys.stdin.readline

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

    G = [[] for _ in range(N)]

    for _ in range(M):

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

        G[u-1].append(v-1)

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

    S, T = S-1, T-1



    q = deque()

    q.append(S)

    cnt = 0

    cnt_k = 0

    visited = [False] * N

    visited[S] = True

    while len(q) > 0:

        cnt += 1

        if cnt % 3 == 0:

            cnt_k += 1



        next_ = set()

        while len(q) > 0:

            n0 = q.pop()

            for n1 in G[n0]:

                if cnt % 3 == 0:

                    if n1 == T:

                        return cnt_k



                    if visited[n1]:

                        continue



                    visited[n1] = True



                next_.add(n1)



        q = deque()

        q.extend(list(next_))



    return -1





if __name__ == '__main__':

    print((main()))

import sys

from collections import deque



def main():

    input = sys.stdin.readline

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

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

    for _ in range(M):

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

        u, v = u-1, v-1



        G[3*u].append(3*v+1)

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

        G[3*u+2].append(3*v)



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

    S, T = 3*(S-1), 3*(T-1)



    cnt = 0

    q = deque()

    q.append(S)

    visited = [False] * (3*N)

    visited[S] = True

    while len(q) > 0:

        q_ = deque()

        cnt += 1

        while len(q) > 0:

            n = q.pop()

            for m in G[n]:

                if visited[m]:

                    continue



                if m == T:

                    return cnt // 3



                visited[m] = True

                q_.append(m)



        q = q_



    return -1





if __name__ == '__main__':

    print((main()))

import sys
from collections import deque

sys.setrecursionlimit(10**6)


def main():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    G = [[] for _ in range(N)]
    for _ in range(M):
        u, v = list(map(int, input().split()))
        G[u - 1].append(v - 1)
    S, T = list(map(int, input().split()))
    S, T = S - 1, T - 1
    q = deque()
    q.append(S)
    cnt = 0
    cnt_k = 0
    visited = [False] * N
    visited[S] = True
    while len(q) > 0:
        cnt += 1
        if cnt % 3 == 0:
            cnt_k += 1
        next_ = set()
        while len(q) > 0:
            n0 = q.pop()
            for n1 in G[n0]:
                if cnt % 3 == 0:
                    if n1 == T:
                        return cnt_k
                    if visited[n1]:
                        continue
                    visited[n1] = True
                next_.add(n1)
        q = deque()
        q.extend(list(next_))
    return -1


if __name__ == "__main__":
    print((main()))

import sys
from collections import deque


def main():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    G = [[] for _ in range(3 * N)]
    for _ in range(M):
        u, v = list(map(int, input().split()))
        u, v = u - 1, v - 1
        G[3 * u].append(3 * v + 1)
        G[3 * u + 1].append(3 * v + 2)
        G[3 * u + 2].append(3 * v)
    S, T = list(map(int, input().split()))
    S, T = 3 * (S - 1), 3 * (T - 1)
    cnt = 0
    q = deque()
    q.append(S)
    visited = [False] * (3 * N)
    visited[S] = True
    while len(q) > 0:
        q_ = deque()
        cnt += 1
        while len(q) > 0:
            n = q.pop()
            for m in G[n]:
                if visited[m]:
                    continue
                if m == T:
                    return cnt // 3
                visited[m] = True
                q_.append(m)
        q = q_
    return -1


if __name__ == "__main__":
    print((main()))

- -sys.setrecursionlimit(10**6) -    G = [[] for _ in range(N)] +    G = [[] for _ in range(3 * N)] -        G[u - 1].append(v - 1) +        u, v = u - 1, v - 1 +        G[3 * u].append(3 * v + 1) +        G[3 * u + 1].append(3 * v + 2) +        G[3 * u + 2].append(3 * v) -    S, T = S - 1, T - 1 +    S, T = 3 * (S - 1), 3 * (T - 1) +    cnt = 0 -    cnt = 0 -    cnt_k = 0 -    visited = [False] * N +    visited = [False] * (3 * N) +        q_ = deque() -        if cnt % 3 == 0: -            cnt_k += 1 -        next_ = set() -            n0 = q.pop() -            for n1 in G[n0]: -                if cnt % 3 == 0: -                    if n1 == T: -                        return cnt_k -                    if visited[n1]: -                        continue -                    visited[n1] = True -                next_.add(n1) -        q = deque() -        q.extend(list(next_)) +            n = q.pop() +            for m in G[n]: +                if visited[m]: +                    continue +                if m == T: +                    return cnt // 3 +                visited[m] = True +                q_.append(m) +        q = q_