CodeEff/ECCO huggingface dataset:
# Original solution
class Node:
    def __init__(self, i):
        self.num = i
        self.ken = set()
        self.kenken = set()
        self.kenkenpa = set()

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

for i in range(M):
    u, v = list(map(int, input().split()))
    u -= 1
    v -= 1
    nodes[v].ken.add(nodes[u])

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

for i in range(N):
    for pre in nodes[i].ken:
        nodes[i].kenken = nodes[i].kenken | pre.ken

for i in range(N):
    for prepre in nodes[i].kenken:
        nodes[i].kenkenpa = nodes[i].kenkenpa | prepre.ken

num = t
step = 0
check_list = [[num, step]]
check = [False] * N
ok = False
while len(check_list) > 0:
    num, step = check_list.pop(0)
    for pre in nodes[num].kenkenpa:
        if pre.num == s:
            print((step+1))
            ok = True
            break
        if not check[pre.num]:
            check[pre.num] = True
            check_list.append([pre.num, step+1])
    if ok:
        break
if not ok:
    print((-1))


# Optimized solution
N, M = list(map(int, input().split()))
nodes = [[] for i in range(N)]

for i in range(M):
    u, v = list(map(int, input().split()))
    u -= 1
    v -= 1
    nodes[u].append(v)

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

num = s
step = 0
q = [[num, step]]
check = [[False for i in range(N)] for j in range(3)]
ok = False
while len(q) > 0:
    num, step = q.pop(0)
    tmp = step % 3
    if check[tmp][num]:
        continue
    if tmp == 0 and num == t:
        print((step//3))
        ok = True
        break
    for next in nodes[num]:
        check[tmp][num] = True
        q.append([next, step+1])
if not ok:
    print((-1))
# Original solution
import heapq as h
import sys

input=sys.stdin.readline

N,M=list(map(int,input().split()))
hen=[[] for i in range(0,N)]
for i in range(0,M):
    u,v=list(map(int,input().split()))
    hen[u-1].append(v-1)

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

triplehen=[[] for i in range(0,N)]
for i in range(0,N):
    q=[i]
    h.heapify(q)
    sub=[]
    h.heapify(sub)
    count=0
    while 3>count:
        while q!=[]:
            x=h.heappop(q)
            for p in hen[x]:
                h.heappush(sub,p)
            if q==[]:
                count+=1
                if count==3:
                    for j in sub:
                        triplehen[i].append(j)
                else:
                    if sub==[]:
                        count=3
                q=sub
                sub=[]
                h.heapify(sub)
                break
triplehen=[list(set(triplehen[i])) for i in range(0,N)]

ans=[-1 for i in range(0,N)]
ans[S-1]=0

q=[]
q.append(S-1)
h.heapify(q)
sub=[]
h.heapify(sub)
count=0
while q!=[]:
    while q!=[]:
        x=h.heappop(q)
        for p in triplehen[x]:
            if ans[p]==-1:
                h.heappush(sub,p)
        if q==[]:
            count+=1
            for j in sub:
                ans[j]=count
            q=sub
            sub=[]
            h.heapify(sub)
            break

print((ans[T-1]))


# Optimized solution
import heapq as h

N,M=list(map(int,input().split()))
hen={(i,j):[] for i in range(0,N) for j in range(0,3)}
for i in range(0,M):
    u,v=list(map(int,input().split()))
    hen[(u-1,0)].append((v-1,1))
    hen[(u-1,1)].append((v-1,2))
    hen[(u-1,2)].append((v-1,0))

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

start=(S-1,0)
goal=(T-1,0)

distance={(i,j):-1 for i in range(0,N) for j in range(0,3)}

q=[]
q.append(start)
distance[start]=0
h.heapify(q)
sub=[]
h.heapify(sub)
while q!=[]:
    while q!=[]:
        x=h.heappop(q)
        for p in hen[x]:
            if distance[p]==-1:
                h.heappush(sub,p)
                distance[p]=distance[x]+1
        #何らかの操作
        #ここでsubキューに次に使うものを格納
        if q==[]:
            q=sub
            sub=[]
            h.heapify(sub)
            break

if distance[goal]==-1:
    print((-1))
else:
    print((distance[goal]//3))
# Original solution
from collections import deque
def bfs(start, goal, g, visited):
    q = deque([start])
    visited[start] = 0
    while q:
        curr_node = q.popleft()
        if curr_node == goal: 
            return
        for next1_node in g[curr_node]:
            for next2_node in g[next1_node]:
                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, visited)
    print((visited[t]))

if __name__ == "__main__":
    main()

# Optimized solution
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()
# Original solution
from heapq import *
import sys
input=sys.stdin.readline
def main():
  N,M=list(map(int,input().split()))
  G=[set() for i in range(N)]
  for i in range(M):
      u,v=list(map(int,input().split()))
      u,v=u-1,v-1
      G[u].add(v)
  S,T=list(map(int,input().split()))
  A=[set() for i in range(N)]
  for i in range(N):
      for v in G[i]:
          A[i]=A[i].union(G[v])
  B=[set() for i in range(N)]
  for i in range(N):
      for v in G[i]:
          B[i]=B[i].union(A[v])
  INF=10**9+7
  def dijkstra(G,s):

    d=[INF]*N
    d[s]=0
    visited={s}
    que=[(0,s)]
    while(que):
      p=heappop(que)
      v=p[1]
      visited.add(v)
      for node in G[v]:
        cost=1
        if (node not in visited) and d[node]>d[v]+cost:
          d[node]=d[v]+cost
          heappush(que,(d[node],node))
    return d
  d=dijkstra(B,S-1)
  if d[T-1]==INF:
    print((-1))
  else:
    print((d[T-1]))
if __name__=='__main__':#おまじない
  main()


# Optimized solution
from heapq import *
import sys
input=sys.stdin.readline
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[3*u+0].append([3*v+1,0])
    G[3*u+1].append([3*v+2,0])
    G[3*u+2].append([3*v+0,1])
S,T=list(map(int,input().split()))
INF=10**9+7
def dijkstra(G,s):

  d=[INF]*(3*N)
  d[s]=0
  visited={s}
  que=[(0,s)]
  while(que):
    p=heappop(que)
    v=p[1]
    visited.add(v)
    for node,cost in G[v]:
      if (node not in visited) and d[node]>d[v]+cost:
        d[node]=d[v]+cost
        heappush(que,(d[node],node))
  return d
d=dijkstra(G,(S-1)*3)
if d[(T-1)*3]==INF:
  print((-1))
else:
  print((d[(T-1)*3]))
# Original solution
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
graph = [[] for _ in range(n)]
for u, v in LIR(m):
    graph[u - 1] += [v - 1]


s, t = LI()
dist = [[INF for _ in range(3)] for _ in range(n)]
dist[s - 1][0] = 0
que = deque([(s - 1, 0)])
while que:
    cur, cost = que.pop()
    for nxt in graph[cur]:
        if cost + 1 < dist[nxt][(cost + 1) % 3]:
            dist[nxt][(cost + 1) % 3] = cost + 1
            que += [(nxt, cost + 1)]



if dist[t - 1][0] == INF:
    print((-1))
else:
    print((dist[t - 1][0] // 3))

# Optimized solution
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
graph = [[] for _ in range(n)]
for u, v in LIR(m):
    graph[u - 1] += [v - 1]


s, t = LI()
dist = [[INF for _ in range(3)] for _ in range(n)]
dist[s - 1][0] = 0
que = deque([(s - 1, 0)])
while que:
    cur, cost = que.popleft()
    for nxt in graph[cur]:
        if cost + 1 < dist[nxt][(cost + 1) % 3]:
            dist[nxt][(cost + 1) % 3] = cost + 1
            que += [(nxt, cost + 1)]



if dist[t - 1][0] == INF:
    print((-1))
else:
    print((dist[t - 1][0] // 3))
# Original solution
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
graph = [[] for _ in range(n)]
for u, v in LIR(m):
    graph[u - 1] += [v - 1]


s, t = LI()
dist = [[INF for _ in range(3)] for _ in range(n)]
dist[s - 1][0] = 0
que = deque([(s - 1, 0)])
while que:
    cur, cost = que.popleft()
    for nxt in graph[cur]:
        if cost + 1 < dist[nxt][(cost + 1) % 3]:
            dist[nxt][(cost + 1) % 3] = cost + 1
            que += [(nxt, cost + 1)]



if dist[t - 1][0] == INF:
    print((-1))
else:
    print((dist[t - 1][0] // 3))

# Optimized solution
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
G = [[] for _ in range(n)]
for _ in range(m):
    x, y = LI()
    G[x - 1] += [y - 1]

dist = [[INF] * n for _ in range(3)]
s, t = LI()
dist[0][s - 1] = 0
dq = deque([(0, s - 1)])
while dq:
    cost, u = dq.popleft()
    for v in G[u]:
        if cost + 1 < dist[(cost + 1) % 3][v]:
            dist[(cost + 1) % 3][v] = cost + 1
            dq += [(cost + 1, v)]


ans = dist[0][t - 1]
if ans == INF:
    print((-1))
else:
    print((ans // 3))
# Original solution
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
G = [[] for _ in range(n)]
for _ in range(m):
    x, y = LI()
    G[x - 1] += [y - 1]

dist = [[INF] * n for _ in range(3)]
s, t = LI()
dist[0][s - 1] = 0
dq = deque([(0, s - 1)])
while dq:
    cost, u = dq.popleft()
    for v in G[u]:
        if cost + 1 < dist[(cost + 1) % 3][v]:
            dist[(cost + 1) % 3][v] = cost + 1
            dq += [(cost + 1, v)]


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





# Optimized solution
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
G = [[] for _ in range(n)]
for x, y in LIR(m):
    G[x - 1] += [y - 1]

dist = [[INF] * n for _ in range(3)]
s, t = LI()
dist[0][s - 1] = 0
dq = deque([(0, s - 1)])
while dq:
    cost, u = dq.popleft()
    for v in G[u]:
        if cost + 1 < dist[(cost + 1) % 3][v]:
            dist[(cost + 1) % 3][v] = cost + 1
            dq += [(cost + 1, v)]


ans = dist[0][t - 1]
if ans == INF:
    print((-1))
else:
    print((ans // 3))
# Original solution
n,m=list(map(int,input().split()))
u,v=[],[]
path=[[] for i in range(n)]
for i in range(m):
    ui,vi=[int(x)-1 for x in input().split()]
    path[ui].append(vi)
    # path[vi].append(ui)
s,t=[int(x)-1 for x in input().split()]
from collections import deque
q=deque()
q.append((s,0))
# print(path)
check=[False]*n
while q:
    # print(q)
    p,num=q.popleft()
    if num%3!=0:
        for i in path[p]:
            q.append((i,num+1))
        continue
    elif p==t:
        print((num//3))
        import sys
        sys.exit()
    elif check[p]==True:
        continue
    else:
        for i in path[p]:
            q.append((i,num+1))
        check[p]=True
print((-1))

# Optimized solution
n,m=list(map(int,input().split()))
u,v=[],[]
path=[[] for i in range(n)]
for i in range(m):
    ui,vi=[int(x)-1 for x in input().split()]
    path[ui].append(vi)
    # path[vi].append(ui)
s,t=[int(x)-1 for x in input().split()]
from collections import deque
q=deque()
q.append((s,0))
# print(path)
check=[[False]*n for x in [0,1,2]]
while q:
    # print(q)
    p,num=q.popleft()
    if check[num%3][p]==True:
        continue
    if num%3!=0:
        for i in path[p]:
            q.append((i,num+1))
        check[num%3][p]=True
        continue
    elif p==t:
        print((num//3))
        import sys
        sys.exit()
    else:
        for i in path[p]:
            q.append((i,num+1))
        check[0][p]=True
print((-1))
# Original solution
#逆探索
N, M = list(map(int, input().split()))
E = []
for i in range(M):
    e = list(map(int, input().split()))
    e.reverse()
    E.append(e)
S, T = list(map(int, input().split()))
keiro = []
for i in range(N):
    keiro.append(set())
for e in E:
    keiro[e[0]-1].add(e[1])
rireki = [{T}]
before = set()
after = {T}
flag = 0
while flag == 0:
    for i in range(3):
        before = after
        after = set()
        for b in before:
            after = after | keiro[b-1]
    after = after | rireki[-1]
    for r in rireki:
        if r == after:
            flag = 1
    if {S} & after == {S}:
        flag = 2
    rireki.append(after | rireki[-1])
    after = after - rireki[-2]
if flag == 1:
    print((-1))
if flag == 2:
    print((len(rireki)-1))


# Optimized solution
#順探索
N, M = list(map(int, input().split()))
E = []
for i in range(M):
    e = list(map(int, input().split()))
    E.append(e)
S, T = list(map(int, input().split()))
keiro = [set()]
for i in range(N):
    keiro.append(set())
for e in E:
    keiro[e[0]].add(e[1])
reached = {S}
before = set()
after = reached
flag = 0 #flag = 0: 探索中 flag = 1: 解無し flag = 2:解発見
counter = 0
while flag == 0:
    for i in range(3):
        before = after
        after = set()
        for b in before:
            after = after | keiro[b]
    counter = counter + 1
    if after - reached == set():
        flag = 1
    if {T} & after == {T}:
        flag = 2
    after = after - reached
    reached = reached | after
if flag == 1:
    print((-1))
if flag == 2:
    print(counter)
# Original solution
from collections import deque

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

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

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

visited = [False] * N
visited[S] = True
queue1 = deque([(0, S)])

while queue1 :
    cost, i = queue1.popleft()
    queue2 = deque([(0, i)])
    
    while queue2 :
        step, q = queue2.popleft()
        if step == 2 : 
            for j in vec[q] :
                if j == T :
                    print((cost + 1))
                    exit()
                if not visited[j] :
                    visited[j] = True
                    queue1.append((cost + 1, j))
        else :
            for j in vec[q] :
                queue2.append((step + 1, j))

print((-1))
    

# Optimized solution
from collections import deque

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

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

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

visited = [[False] * N for _ in range(3)]
visited[0][S] = True
queue = deque([(S, 0)])

while queue :
    q, cost = queue.popleft()
    p = (cost + 1) % 3
    for i in vec[q] :
        if p == 0 and i == T :
            print(((cost + 1) // 3))
            exit()
        if not visited[p][i] :
            visited[p][i] = True
            queue.append((i, cost + 1))

print((-1))
# Original solution
from collections import deque

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

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

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

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

inf = 10**6
stack = deque([[3*S,0]])
time = [inf] * (3*N)

while stack:
    x = stack.pop()
    for y in data[x[0]]:
        if time[y] > x[1] + 1:
            time[y] = x[1] + 1
            stack.append([y,x[1] + 1])

if time[3*T] == inf:
    print((-1))
else:
    print((time[3*T]//3))

# Optimized solution
from collections import deque

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

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

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

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

inf = 10**6
stack = deque([[3*S,0]])
time = [inf] * (3*N)

while stack:
    x = stack.popleft()
    for y in data[x[0]]:
        if time[y] > x[1] + 1:
            time[y] = x[1] + 1
            stack.append([y,x[1] + 1])

if time[3*T] == inf:
    print((-1))
else:
    print((time[3*T]//3))
# Original solution
from collections import defaultdict, deque
import sys
sys.setrecursionlimit(10**8)

N, M = list(map(int, input().split()))
e = defaultdict(list)
for i in range(M):
    a, b = list(map(int, input().split()))
    e[(a, 0)].append((b, 1))
    e[(a, 1)].append((b, 2))
    e[(a, 2)].append((b, 0))

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

d = deque()
d.appendleft((S, 0))

inf = float('inf')
dist = defaultdict(lambda: inf)
dist[(S, 0)] = 0

def BFS(now):
    for v in e[now]:
        if(dist[v] > dist[now] + 1):
            dist[v] = dist[now] + 1
            if(v == (T, 0)):
                return
            BFS(v)
    return

BFS((S, 0))

ans = dist[(T, 0)]
if(ans == inf):
    print((-1))
else:
    print((ans//3))



# Optimized solution
from collections import deque, defaultdict

N, M = list(map(int, input().split()))
edge = defaultdict(list)
for i in range(M):
    a, b = list(map(int, input().split()))
    edge[(a, 0)].append((b, 1))
    edge[(a, 1)].append((b, 2))
    edge[(a, 2)].append((b, 0))

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

inf = float('inf')
dist = {}
for i in range(1, N+1):
    dist[(i, 0)] = inf
    dist[(i, 1)] = inf
    dist[(i, 2)] = inf
dist[(S, 0)] = 0

dq = deque()
dq.append((S, 0))

while dq:
    v = dq.popleft()
    if v == (T, 0):
        break
    for u in edge[v]:
        if dist[u] < inf: continue
        dq.append(u)
        dist[u] = dist[v] + 1

if(dist[(T, 0)] == inf):
    print((-1))
else:
    print((dist[(T, 0)]//3))