N, M = [int(x) for x in input().strip().split(" ")]

from collections import deque

vs = set()

def bfs(v,d,t,N):

    queue=deque([v])

    dist = [0] * (N+1)

    while True:

        if len(queue) == 0:

            break

        v = queue.popleft()

        for u in d[v]:

            if u in vs:

                continue

            vs.add(u)

            dist[u] = dist[v] + 1

            if u == t:

                return dist[u]//3

            queue.append(u)

    return -1



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

for _ in range(M):

    a, b = [int(x) for x in input().strip().split(" ")]

    d[a].add(N+b)

    d[N+a].add(2*N+b)

    d[2*N+a].add(b)



s, t = [int(x) for x in input().strip().split(" ")]

print((bfs(s, d, t, 3*N+1)))

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

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

inf=10**20

c=[[] for i in range(N*3+1)]

for i,j in edge:

    c[i].append(N+j)

    c[N+i].append(N+N+j)

    c[N+N+i].append(j)

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

from collections import deque

d=[inf]*(N*3+1)

d[S]=0

q=deque([S])

x=1

while q:

    q2=deque()

    for i in q:

        for j in c[i]:

            if d[j]>x:

                d[j]=x

                q2.append(j)

    q=q2

    x+=1

print((d[T]//3 if d[T]<inf else -1))

N, M = [int(x) for x in input().strip().split(" ")]
from collections import deque

vs = set()


def bfs(v, d, t, N):
    queue = deque([v])
    dist = [0] * (N + 1)
    while True:
        if len(queue) == 0:
            break
        v = queue.popleft()
        for u in d[v]:
            if u in vs:
                continue
            vs.add(u)
            dist[u] = dist[v] + 1
            if u == t:
                return dist[u] // 3
            queue.append(u)
    return -1


d = [set() for _ in range(3 * N + 1)]
for _ in range(M):
    a, b = [int(x) for x in input().strip().split(" ")]
    d[a].add(N + b)
    d[N + a].add(2 * N + b)
    d[2 * N + a].add(b)
s, t = [int(x) for x in input().strip().split(" ")]
print((bfs(s, d, t, 3 * N + 1)))

N, M = list(map(int, input().split()))
edge = [list(map(int, input().split())) for i in range(M)]
inf = 10**20
c = [[] for i in range(N * 3 + 1)]
for i, j in edge:
    c[i].append(N + j)
    c[N + i].append(N + N + j)
    c[N + N + i].append(j)
S, T = list(map(int, input().split()))
from collections import deque

d = [inf] * (N * 3 + 1)
d[S] = 0
q = deque([S])
x = 1
while q:
    q2 = deque()
    for i in q:
        for j in c[i]:
            if d[j] > x:
                d[j] = x
                q2.append(j)
    q = q2
    x += 1
print((d[T] // 3 if d[T] < inf else -1))

-N, M = [int(x) for x in input().strip().split(" ")] +N, M = list(map(int, input().split())) +edge = [list(map(int, input().split())) for i in range(M)] +inf = 10**20 +c = [[] for i in range(N * 3 + 1)] +for i, j in edge: +    c[i].append(N + j) +    c[N + i].append(N + N + j) +    c[N + N + i].append(j) +S, T = list(map(int, input().split())) -vs = set() - - -def bfs(v, d, t, N): -    queue = deque([v]) -    dist = [0] * (N + 1) -    while True: -        if len(queue) == 0: -            break -        v = queue.popleft() -        for u in d[v]: -            if u in vs: -                continue -            vs.add(u) -            dist[u] = dist[v] + 1 -            if u == t: -                return dist[u] // 3 -            queue.append(u) -    return -1 - - -d = [set() for _ in range(3 * N + 1)] -for _ in range(M): -    a, b = [int(x) for x in input().strip().split(" ")] -    d[a].add(N + b) -    d[N + a].add(2 * N + b) -    d[2 * N + a].add(b) -s, t = [int(x) for x in input().strip().split(" ")] -print((bfs(s, d, t, 3 * N + 1))) +d = [inf] * (N * 3 + 1) +d[S] = 0 +q = deque([S]) +x = 1 +while q: +    q2 = deque() +    for i in q: +        for j in c[i]: +            if d[j] > x: +                d[j] = x +                q2.append(j) +    q = q2 +    x += 1 +print((d[T] // 3 if d[T] < inf else -1))