CodeEff/ECCO huggingface dataset:
# Original solution
from queue import Queue
N, M = list(map(int, input().split()))
INF = -1
dist = [[INF]*3 for _ in range(N)]
vector = [[] for _ in range(N)]

for i in range(M):
    graf1, graf2 = list(map(int, input().split()))
    vector[graf1-1].append(graf2-1)
S, T = list(map(int, input().split()))
S, T = S-1, T-1

# BFS
dist[S][0] = 0
q = Queue(3*N+1)
q.put((S, 0))
while not q.empty():
    v, l = q.get()
    for i in vector[v]:
        new_l = (l+1)%3
        if dist[i][new_l] != INF:
            continue
        dist[i][new_l] = dist[v][l] + 1
        q.put((i, new_l))

ans = dist[T][0]
if ans == INF:
    print("-1")
else:
    print((int(ans/3)))

# Optimized solution
from collections import deque
N, M = list(map(int, input().split()))
INF = -1
dist = [[INF]*3 for _ in range(N)]
vector = [[] for _ in range(N)]

for i in range(M):
    graf1, graf2 = list(map(int, input().split()))
    vector[graf1-1].append(graf2-1)
S, T = list(map(int, input().split()))
S, T = S-1, T-1

# BFS
dist[S][0] = 0
q = deque()
q.append((S, 0))
while q:
    v, l = q.popleft()
    for i in vector[v]:
        new_l = (l+1)%3
        if dist[i][new_l] != INF:
            continue
        dist[i][new_l] = dist[v][l] + 1
        q.append((i, new_l))

ans = dist[T][0]
if ans == INF:
    print("-1")
else:
    print((int(ans/3)))
# Original solution
from collections import deque
N,M,*L,S,T = list(map(int,open(0).read().split()))
G = [[] for _ in range(N)]
step = [[-1]*3 for _ in range(N)]
step[S-1][0] = 0
for u,v in zip(*[iter(L)]*2):
	G[u-1].append(v-1)
q = deque([(S-1,0)])
while q:
	cur,d = q.popleft()
	for nxt in G[cur]:
		if nxt==T-1 and (d+1)%3==0:
			print(((d+1)//3))
			exit()
		if step[nxt][(d+1)%3] < 0:
			step[nxt][(d+1)%3] = d+1
			q.append((nxt,d+1))
print((-1))

# Optimized solution
from collections import deque
N,M,*L,S,T = list(map(int,open(0).read().split()))
G = [[] for _ in range(N)]
step = [False for _ in range(3*N)]
step[(S-1)*3] = 0
for u,v in zip(*[iter(L)]*2):
	G[u-1].append(v-1)
q = deque([(S-1,0)])
while q:
	cur,d = q.popleft()
	for nxt in G[cur]:
		if nxt==T-1 and (d+1)%3==0:
			print(((d+1)//3))
			exit()
		if not step[nxt*3+(d+1)%3]:
			step[nxt*3+(d+1)%3] = True
			q.append((nxt,d+1))
print((-1))
# Original solution
from collections import deque
N,M,*L,S,T = list(map(int,open(0).read().split()))
G = [[] for _ in range(N)]
step = [0 for _ in range(N)]
step[S-1] = 1
for u,v in zip(*[iter(L)]*2):
	G[u-1].append(v-1)
q = deque([(S-1,0)])
while q:
	cur,d = q.popleft()
	for nxt in G[cur]:
		if nxt==T-1 and (d+1)%3==0:
			print(((d+1)//3))
			exit()
		if not step[nxt] & 1<<((d+1)%3):
			step[nxt] |= 1<<(d+1)%3
			q.append((nxt,d+1))
print((-1))

# Optimized solution
def main():
	from collections import deque
	N,M,*L,S,T = list(map(int,open(0).read().split()))
	G = [[] for _ in range(N)]
	step = [False]*(3*N)
	step[(S-1)*3] = True
	for u,v in zip(*[iter(L)]*2):
		G[u-1].append(v-1)
	q = deque([(S-1,0)])
	while q:
		cur,d = q.popleft()
		for nxt in G[cur]:
			if nxt==T-1 and (d+1)%3==0:
				print(((d+1)//3))
				exit()
			if not step[nxt*3+(d+1)%3]:
				step[nxt*3+(d+1)%3] = True
				q.append((nxt,d+1))
	print((-1))
	
if __name__=="__main__":main()
# Original solution
from collections import deque
n,m = list(map(int,input().split()))
es = [[] for _ in range(n+1)]
for _ in range(m):
    u,v = list(map(int,input().split()))
    es[u].append(v)
to = [[] for _ in range(n+1)]
for i in range(1, n+1):
    que = deque([])
    for j in es[i]:
        que.append((j, 1))
    while que:
        v,c = que.popleft()
        if c > 3:
            continue
        elif c==3:
            if v  not in to[i]:
                to[i].append(v)
        else:
            for j in es[v]:
                que.append((j, c+1))
s,t = list(map(int,input().split()))
visited = [False]*(n+1)
que = deque([])
for st in to[s]:
    que.append((st, 1))
ans = -1
while que:
    v,c = que.popleft()
    if visited[v]:
        continue
    elif v==t:
        ans = c
        break
    else:
        visited[v] = True
        for nx in to[v]:
            que.append((nx,c+1))
print(ans)

# Optimized solution
n,m = list(map(int,input().split()))
if m==0:
    print((-1))
    exit()
es = [[] for _ in range(n+1)]
for _ in range(m):
    u,v = list(map(int,input().split()))
    es[u].append(v)
s,t = list(map(int,input().split()))

INF = 10**9
dist = [[INF,INF,INF] for _ in range(n+1)]

d = 0
q = [s]

while q:
    d += 1
    r = d%3
    qq = []
    for u in q:
        for v in es[u]:
            if dist[v][r] == INF:
                dist[v][r] = d
                qq.append(v)
    q = qq

d = dist[t][0]
print((-1 if d==INF else d//3))
# Original solution
import sys
input = sys.stdin.readline

N, M = list(map(int, input().split()))
# N-1辺表示をグラフ表示に
graph = [[] for _ in range(N)]
for _ in range(M):
    a, b = list(map(int, input().split()))
    graph[a-1].append(b-1)
S, T = [int(x)-1 for x in input().split()]


q = [S]
checked = [False]*N
checked[S] = True

ok = False
c = 0
while q:
    c += 1
    qq = []
    if c % 3 == 0:
        for p in q:
            for np in graph[p]:
                if np == T:
                    ok = True
                    break
                if not checked[np]:
                    qq.append(np)
                    checked[np] = True
    else:
        for p in q:
            for np in graph[p]:
                qq.append(np)
    if ok:
        break
    q = qq

if ok:
    print((c//3))
else:
    print((-1))

# Optimized solution
import sys
input = sys.stdin.readline

N, M = list(map(int, input().split()))
# N-1辺表示をグラフ表示に
graph = [[] for _ in range(N)]
for _ in range(M):
    a, b = list(map(int, input().split()))
    graph[a-1].append(b-1)
S, T = [int(x)-1 for x in input().split()]


q = {S}
checked = [False]*N
checked[S] = True

ok = False
c = 0
while q:
    c += 1
    qq = set()
    if c % 3 == 0:
        for p in q:
            for np in graph[p]:
                if np == T:
                    ok = True
                    break
                if not checked[np]:
                    qq.add(np)
                    checked[np] = True
    else:
        for p in q:
            for np in graph[p]:
                qq.add(np)
    if ok:
        break
    q = qq

if ok:
    print((c//3))
else:
    print((-1))
# Original solution
import sys
input = sys.stdin.readline

N, M = list(map(int, input().split()))
# N-1辺表示をグラフ表示に
graph = [[] for _ in range(N)]
for _ in range(M):
    a, b = list(map(int, input().split()))
    graph[a-1].append(b-1)
S, T = [int(x)-1 for x in input().split()]


q = {S}
checked = [False]*N
checked[S] = True

ok = False
c = 0
while q:
    c += 1
    qq = set()
    if c % 3 == 0:
        for p in q:
            for np in graph[p]:
                if np == T:
                    ok = True
                    break
                if not checked[np]:
                    qq.add(np)
                    checked[np] = True
    else:
        for p in q:
            for np in graph[p]:
                qq.add(np)
    if ok:
        break
    q = qq

if ok:
    print((c//3))
else:
    print((-1))

# Optimized solution
import sys
input = sys.stdin.readline

N, M = list(map(int, input().split()))
graph = [[] for _ in range(N)]
for _ in range(M):
    a, b = list(map(int, input().split()))
    graph[a-1].append(b-1)
    #graph[b-1].append(a-1)
S, T = list(map(int, input().split()))
S -= 1; T -= 1

D = [[-1]*3 for _ in range(N)]
D[S][0] = 0
q = [(S, 0)]
while q:
    qq = []
    for p, d in q:
        for np in graph[p]:
            nd = (d+1)%3
            if D[np][nd] == -1:
                D[np][nd] = D[p][d] + 1
                qq.append((np, nd))
    q = qq

if D[T][0] == -1:
    print((-1))
else:
    print((D[T][0]//3))
# Original solution
import sys

input = sys.stdin.readline
n,m = list(map(int,input().split()))
from collections import defaultdict
dic = defaultdict(list)

for _ in range(m):
    u,v = list(map(int,input().split()))
    dic[u-1].append(v-1)

G = defaultdict(list)
def dfs(p, s, l):
    if l == 3:
        if s != p:
            G[s].append(p)
    else:
        for i in dic[p]:
            dfs(i, s, l+1)

for i in range(n):
    dfs(i, i, 0)
s,t = list(map(int,input().split()))

# G[v]: 頂点vに隣接する頂点list
# N: 頂点数

from collections import deque
dist = [-1]*n
que = deque([s-1])
dist[s-1] = 0
while que:
    v = que.popleft()
    d = dist[v]
    for w in G[v]:
        if dist[w] > -1:
            continue
        dist[w] = d + 1
        que.append(w)
print((dist[t-1]))

# Optimized solution
import sys

input = sys.stdin.readline
n,m = list(map(int,input().split()))
from collections import defaultdict
dic = defaultdict(list)

for _ in range(m):
    u,v = list(map(int,input().split()))
    dic[u-1].append(v-1+n)
    dic[u - 1+n].append(v - 1 + 2*n)
    dic[u - 1+2*n].append(v - 1)

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

# G[v]: 頂点vに隣接する頂点list
# N: 頂点数

from collections import deque
dist = [-1]*(n*3)
que = deque([s-1])
dist[s-1] = 0
while que:
    v = que.popleft()
    d = dist[v]
    for w in G[v]:
        if dist[w] > -1:
            continue
        dist[w] = d + 1
        que.append(w)
print((dist[t-1]//3))
# Original solution
from collections import defaultdict, deque


def bfs(V, s, t):
    q = deque([(0, s)])
    used = set()
    
    while q:
        dist, a = q.popleft()
        used.add(a)
        if a == t:
            break
        for b in V[a]:
            if b in used: continue
            q.append((dist+1, b))
    
    if a != t:
        dist = -1
    else:
        dist = -1 if dist%3 else dist//3
    return dist


if __name__ == "__main__":
    V = defaultdict(list)
    N, M = list(map(int, input().split()))
    for _ in range(M):
        u, v = list(map(int, input().split()))
        V[u].append(v*(N+1))
        V[u*(N+1)].append(v*(2*N+1))
        V[u*(2*N+1)].append(v)

    S, T = list(map(int, input().split()))
    dist = bfs(V, S, T)
    print(dist)


# Optimized solution
from collections import defaultdict, deque
from heapq import heappush, heappop


def bfs(V, s, t):
    q = deque([(0, s)])
    used = set()
    
    while q:
        dist, a = q.popleft()
        used.add(a)
        if a == t:
            break
        for b in V[a]:
            if b in used: continue
            q.append((dist+1, b))
    
    if a != t:
        dist = -1
    else:
        dist = -1 if dist%3 else dist//3
    return dist

def dijkstra1(V, n, source):
    # O(ElogV)

    inf = float("inf")
    dist = [inf] * (n+1)
    dist[source] = 0
    q = [(0, source)]

    while q:
        cost, u = heappop(q)
        if dist[u] < cost:
            continue
        for c, v in V[u]:
            if dist[v] > dist[u] + c:
                dist[v] = dist[u] + c
                heappush(q, (dist[v], v))

    return dist


if __name__ == "__main__":
    V = defaultdict(list)
    N, M = list(map(int, input().split()))
    for _ in range(M):
        u, v = list(map(int, input().split()))
        V[u].append((1, v+(N)))
        V[u+(N)].append((1, v+(2*N)))
        V[u+(2*N)].append((1, v))

    S, T = list(map(int, input().split()))
    dist = dijkstra1(V, 3*N, S)[T]
    print((-1 if dist==float("inf") else dist//3))
# Original solution
from collections import defaultdict, deque
from heapq import heappush, heappop


def bfs(V, s, t):
    q = deque([(0, s)])
    used = set()
    
    while q:
        dist, a = q.popleft()
        used.add(a)
        if a == t:
            break
        for b in V[a]:
            if b in used: continue
            q.append((dist+1, b))
    
    if a != t:
        dist = -1
    else:
        dist = -1 if dist%3 else dist//3
    return dist

def dijkstra1(V, n, source):
    # O(ElogV)

    inf = float("inf")
    dist = [inf] * (n+1)
    dist[source] = 0
    q = [(0, source)]

    while q:
        cost, u = heappop(q)
        if dist[u] < cost:
            continue
        for c, v in V[u]:
            if dist[v] > dist[u] + c:
                dist[v] = dist[u] + c
                heappush(q, (dist[v], v))

    return dist


if __name__ == "__main__":
    V = defaultdict(list)
    N, M = list(map(int, input().split()))
    for _ in range(M):
        u, v = list(map(int, input().split()))
        V[u].append((1, v+(N)))
        V[u+(N)].append((1, v+(2*N)))
        V[u+(2*N)].append((1, v))

    S, T = list(map(int, input().split()))
    dist = dijkstra1(V, 3*N, S)[T]
    print((-1 if dist==float("inf") else dist//3))

# Optimized solution
from heapq import heappush, heappop

def dijkstra1(V, n, source):
    # O(ElogV)

    inf = float("inf")
    dist = [inf] * (n+1)
    dist[source] = 0
    q = [(0, source)]

    while q:
        cost, u = heappop(q)
        if dist[u] < cost:
            continue
        for c, v in V[u]:
            if dist[v] > dist[u] + c:
                dist[v] = dist[u] + c
                heappush(q, (dist[v], v))

    return dist


if __name__ == "__main__":
    N, M = list(map(int, input().split()))
    V = {i:[] for i in range(1, 3*N+1)}
    for _ in range(M):
        u, v = list(map(int, input().split()))
        V[u].append((1, v+(N)))
        V[u+(N)].append((1, v+(2*N)))
        V[u+(2*N)].append((1, v))

    S, T = list(map(int, input().split()))
    dist = dijkstra1(V, 3*N, S)[T]
    print((-1 if dist==float("inf") else dist//3))
# Original solution
from heapq import heappush, heappop

def dijkstra1(V, n, source):
    # O(ElogV)

    inf = float("inf")
    dist = [inf] * (n+1)
    dist[source] = 0
    q = [(0, source)]

    while q:
        cost, u = heappop(q)
        if dist[u] < cost:
            continue
        for c, v in V[u]:
            if dist[v] > dist[u] + c:
                dist[v] = dist[u] + c
                heappush(q, (dist[v], v))

    return dist


def solve():
    N, M = list(map(int, input().split()))
    V = {i:[] for i in range(1, 3*N+1)}
    for _ in range(M):
        u, v = list(map(int, input().split()))
        V[u].append((1, v+(N)))
        V[u+(N)].append((1, v+(2*N)))
        V[u+(2*N)].append((1, v))

    S, T = list(map(int, input().split()))
    dist = dijkstra1(V, 3*N, S)[T]
    print((-1 if dist==float("inf") else dist//3))


if __name__ == "__main__":
    solve()

# Optimized solution
from heapq import heappush, heappop

def dijkstra1(V, n, inf, source):
    # O(ElogV)

    dist = [inf] * (n+1)
    dist[source] = 0
    q = [(0, source)]

    while q:
        cost, u = heappop(q)
        if dist[u] < cost:
            continue
        for c, v in V[u]:
            if dist[v] > dist[u] + c:
                dist[v] = dist[u] + c
                heappush(q, (dist[v], v))

    return dist


def solve():
    INF = 10**25
    N, M = list(map(int, input().split()))
    V = {i:[] for i in range(1, 3*N+1)}
    for _ in range(M):
        u, v = list(map(int, input().split()))
        V[u].append((1, v+(N)))
        V[u+(N)].append((1, v+(2*N)))
        V[u+(2*N)].append((1, v))

    S, T = list(map(int, input().split()))
    dist = dijkstra1(V, 3*N, INF, S)[T]
    print((-1 if dist==INF else dist//3))


if __name__ == "__main__":
    solve()
# Original solution
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))

# Optimized solution
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)))
# Original solution
n,m=list(map(int,input().split()))
g=[[] for _ in range(n)]
for _ in range(m):
  a,b=list(map(int,input().split()))
  a,b=a-1,b-1
  g[a].append(b)
s,t=list(map(int,input().split()))
s,t=s-1,t-1
gg=[set(()) for _ in range(n)]
for i in range(n):
  for j in g[i]:
    for k in g[j]:
      for l in g[k]:
        if l!=i:
          gg[i].add(l)
from collections import deque
todo=deque([s])
seen=[-1]*n
seen[s]=0
while todo:
  v=todo.popleft()
  for l in gg[v]:
    if seen[l]==-1:
      todo.append(l)
      seen[l]=seen[v]+1
print((seen[t]))

# Optimized solution
n,m=list(map(int,input().split()))
g=[[] for _ in range(n)]
gg=[[] for _ in range(3*n)]
for _ in range(m):
  a,b=list(map(int,input().split()))
  a,b=a-1,b-1
  gg[a].append(n+b)
  gg[n+a].append(2*n+b)
  gg[2*n+a].append(b)
s,t=list(map(int,input().split()))
s,t=s-1,t-1
from collections import deque
todo=deque([s])
seen=[-1]*(3*n)
seen[s]=0
while todo:
  v=todo.popleft()
  for l in gg[v]:
    if seen[l]==-1:
      todo.append(l)
      seen[l]=seen[v]+1
      if l==t:
        print((seen[l]//3))
        exit()
if seen[t]>0:
  print((seen[t]//3))
else:
  print((-1))
# Original solution
import sys
from collections import deque


data = sys.stdin.readlines()
N, M = list(map(int, data[0].split()))
edge = [[] for _ in range(N)]
for i in range(1, M + 1):
    u, v = list(map(int, data[i].split()))
    u -= 1
    v -= 1
    edge[u].append(v)
S, T = [int(x) - 1 for x in data[M + 1].split()]


def bfs1(s):
    q = deque()
    q.append((s, 0))

    while len(q) > 0:
        v, step = q.popleft()
        if step == 3:
            edge3[s].append(v)
        else:
            for nv in edge[v]:
                q.append((nv, step + 1))


def bfs2(n):
    global T
    q = deque()
    status[n] = 0
    q.append(n)

    while len(q) > 0:
        v = q.popleft()
        for nv in edge3[v]:
            if status[nv] == -1:
                status[nv] = status[v] + 1
                q.append(nv)


edge3 = [[] for _ in range(N)]
for i in range(N):
    bfs1(i)


status = [-1] * N
status[S] = 0
bfs2(S)
print((status[T]))


# Optimized solution
import sys
from collections import deque


data = sys.stdin.readlines()
N, M = list(map(int, data[0].split()))
edge = [[] for _ in range(N)]
for i in range(1, M + 1):
    u, v = list(map(int, data[i].split()))
    u -= 1
    v -= 1
    edge[u].append(v)
S, T = [int(x) - 1 for x in data[M + 1].split()]

visited = set()
q = deque()
q.append((0, S))
ans = 10 ** 10
while len(q) > 0:
    c, v = q.popleft()
    key = (c % 3, v)
    if key in visited:
        continue
    visited.add(key)
    if v == T and c % 3 == 0:
        ans = min(ans, c // 3)
    else:
        for e in edge[v]:
            newKey = (c + 1, e)
            q.append(newKey)

if ans == 10 ** 10:
    print((-1))
else:
    print(ans)
# Original solution
from collections import*
n,m,*w,s,t=list(map(int,open(0).read().split()))
e=[[]for _ in range(n+1)]
for u,v in zip(w[::2],w[1::2]):e[u].append(v)
f=[[-3]*3for _ in range(n+1)]
f[s][0]=0
q=deque([(s,0)])
while q:
  v,m=q.popleft()
  for w in e[v]:
    if f[w][-~m%3]<0:
      f[w][-~m%3]=f[v][m]+1
      q+=[(w,-~m%3)]
print((f[t][0]//3))

# Optimized solution
from collections import*
n,m,*w,s,t=list(map(int,open(0).read().split()))
e=[[]for _ in[0]*-~n]
for u,v in zip(w[::2],w[1::2]):e[u]+=v,
f=[-3]*n*4
f[s*3]=0
q=deque([(s,0)])
while q:
  v,m=q.popleft()
  u=-~m%3
  for w in e[v]:
    if f[w*3+u]<0:
      f[w*3+u]=f[v*3+m]+1
      q+=(w,u),
print((f[t*3]//3))
# Original solution
from collections import deque

n, m = list(map(int, input().split()))
uv  =[[] for _ in range(n)]
for _ in range(m):
    u, v = list(map(int, input().split()))
    uv[u - 1].append(v - 1)
s, t = list(map(int, input().split()))
s -= 1
t -= 1
uv2 = [[] for _ in range(n)]
uv3 = [[] for _ in range(n)]

for i, lst in enumerate(uv):
    for j in lst:
        lst2 = uv[j]
        for k in lst2:
            uv2[i].append(k)
            
for i, lst in enumerate(uv):
    for j in lst:
        lst2 = uv2[j]
        for k in lst2:
            uv3[i].append(k)

queue = deque()
queue.append(s)
queue.append(1)
check = [False for _ in range(n)]
check[s] = True

while queue:
    pos = queue.popleft()
    ans = queue.popleft()
    for next_pos in uv3[pos]:
        if next_pos == t:
            print(ans)
            exit()
        if check[next_pos]:
            continue
        check[next_pos] = True
        queue.append(next_pos)
        queue.append(ans + 1)
print((-1))
    

# Optimized solution
from collections import deque

n, m = list(map(int, input().split()))
uv  =[[] for _ in range(n)]
for _ in range(m):
    u, v = list(map(int, input().split()))
    uv[u - 1].append(v - 1)
s, t = list(map(int, input().split()))
s -= 1
t -= 1

queue = deque()
queue.append(s)
queue.append(1)
queue.append(0)
check = [[False for _ in range(3)] for _ in range(n)]
check[s][0] = True

while queue:
    pos = queue.popleft()
    ans = queue.popleft()
    kkp = queue.popleft()
    for next_pos in uv[pos]:
        if next_pos == t and kkp == 2:
            print((ans // 3))
            exit()
        if check[next_pos][(kkp + 1) % 3]:
            continue
        check[next_pos][(kkp + 1) % 3] = True
        queue.append(next_pos)
        queue.append(ans + 1)
        queue.append((kkp + 1) % 3)
print((-1))
# Original solution
N, M = list(map(int,input().split()))
L1 = [set() for k in range(3*(N+1))]
for k in range(M):
	u, v = list(map(int, input().split()))
	L1[3*u].add(3*v+1)
	L1[3*u+1].add(3*v+2)
	L1[3*u+2].add(3*v)
S, T = list(map(int, input().split()))
S = 3*S
T = set([3*T])

t = 0
sumi = set([S])
now = set([S])
flg = 0
while(len(now) != 0):
	t += 1
	pre = set()
	for n in now:
		pre = pre.union(L1[n])
	now = pre - sumi
	sumi = sumi.union(pre)
	if T <= pre:
		print((t//3))
		flg =1
		break
if flg == 0:
	print((-1))

# Optimized solution
N, M = list(map(int,input().split()))
L1 = [[] for k in range(3*(N+1))]
for k in range(M):
	u, v = list(map(int, input().split()))
	L1[3*u].append(3*v+1)
	L1[3*u+1].append(3*v+2)
	L1[3*u+2].append(3*v)
S, T = list(map(int, input().split()))
S = 3*S
T = 3*T

t = 0
sumi = [None for k in range(3*(N+1))]
sumi[T] = -1
now = [S]
flg = 0
while(len(now) != 0):
	t += 1
	pre = []
	for n in now:
		pre.extend(L1[n])
	now = []
	for p in pre:
		if sumi[p] == None:
			sumi[p] = t
			now.append(p)
		elif sumi[p] == -1:
			flg = 1
	if flg == 1:
		print((t//3))
		flg = 2
		break
if flg == 0:
	print((-1))