CodeEff/ECCO huggingface dataset:
# Original solution
#!/usr/bin python3
# -*- coding: utf-8 -*-

# 双方向グラフで幅優先探索

from collections import deque

n, m = list(map(int, input().split()))
g = [set([]) for _ in range(n)]
#隣接リストの作成
for i in range(m):
    a, b = list(map(int, input().split()))
    a, b = a-1, b-1
    g[a].add(b)

graph = [set([]) for _ in range(n)]
for i in range(n):
    g1 = set([])
    g2 = set([])
    g3 = set([])
    g1 = g1 | g[i]
    for x in g1:
        g2 = g2 | g[x]
    for x in g2:
        g3 = g3 | g[x]
    graph[i] = g3

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

#幅優先探索
q = deque()
level = [-1]*n
q.append(s)
level[s] = 0
while len(q)>0:
    cur = q.popleft()
    lvl = level[cur]
    for i in graph[cur]:
        if level[i]==-1:
            #階層
            level[i] = lvl + 1
            q.append(i)
print((level[t]))


# Optimized solution
#!/usr/bin python3
# -*- coding: utf-8 -*-

# 双方向グラフで幅優先探索

from collections import deque

n, m = list(map(int, input().split()))
g = [set([]) for _ in range(n)]
#隣接リストの作成
for i in range(m):
    a, b = list(map(int, input().split()))
    a, b = a-1, b-1
    g[a].add(b)

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

#幅優先探索
q = deque()
level = [[-1]*n for i in range(3)]
q.append((s, 0))
level[0][s] = 0
while len(q)>0:
    cur, hp = q.popleft()
    lvl = level[hp][cur]
    for i in g[cur]:
        if level[(hp+1)%3][i]==-1:
            #階層
            level[(hp+1)%3][i] = lvl + 1
            q.append((i, (hp+1)%3))
print((level[0][t]//3))
# Original solution
import queue
INF=-1

N,M=[int(e) for e in input().split(' ')]

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

for i in range(M):
    u,v=[int(e) for e in input().split(' ')]
    to[u-1].append(v-1)
S,T=[int(e)-1 for e in input().split(' ')]

dist=[[INF for i in range(3)] for j in range(N)]
q=queue.Queue()
q.put([S,0])
dist[S][0]=0

while(not q.empty()):
    v,l=q.get()
    for v_to in to[v]:
        nl=(l+1)%3
        if dist[v_to][nl] != INF:
            continue
        dist[v_to][nl]=dist[v][l]+1
        q.put([v_to,nl])
ans=dist[T][0]
if ans == INF:
    ans = -1
else:
    ans=ans//3

print(ans)


# Optimized solution
#import queue
from collections import deque
INF=-1

N,M=[int(e) for e in input().split(' ')]

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

for i in range(M):
    u,v=[int(e) for e in input().split(' ')]
    to[u-1].append(v-1)
S,T=[int(e)-1 for e in input().split(' ')]

dist=[[INF for i in range(3)] for j in range(N)]

q=deque([])
q.append([S,0])

dist[S][0]=0

while(q):
    v,l=q.popleft()
    for v_to in to[v]:
        nl=(l+1)%3
        if dist[v_to][nl] != INF:
            continue
        dist[v_to][nl]=dist[v][l]+1
        q.append([v_to,nl])
ans=dist[T][0]
if ans == INF:
    ans = -1
else:
    ans=ans//3

print(ans)
# Original solution
from collections import deque
def sol():
    n,m=list(map(int,input().split()))
    d={}
    for i in range(m):
        u,v=list(map(int,input().split()))
        if u in d:
            d[u].append(v)
        else:
            d[u]=[v]
    s,t=list(map(int,input().split()))
    ans=chk=0
    w=deque([0,s])
    tmp=[0]*(n+1)
    ans=1
    n+=n//2
    n*=2
    while n:
        x=w.pop()
        if x==0:
            ans+=1
            n-=1
            w.appendleft(0)
        elif x in d:
            for j in d[x]:
                if tmp[j]==0 or tmp[j]%3!=ans%3:
                    w.appendleft(j)
                    tmp[j]=ans
        if ans%3==0 and tmp[t]==ans:
            print((ans//3))
            exit()
    print((-1))

if __name__=="__main__":
    sol()


# Optimized solution
from collections import deque
def sol():
    n,m=list(map(int,input().split()))
    d={}
    for i in range(m):
        u,v=list(map(int,input().split()))
        if u in d:
            d[u].append(v)
        else:
            d[u]=[v]
    s,t=list(map(int,input().split()))
    ans=chk=0
    w=deque([0,s])
    #tmp=[0]*(n+1)
    tmp=[[0,0,0] for i in range(n+1)]
    ans=1
    n+=n//2
    n*=2
    while n:
        x=w.pop()
        if x==0:
            ans+=1
            n-=1
            w.appendleft(0)
        elif x in d:
            for j in d[x]:
                if ans%3==0 and j==t:
                    print((ans//3))
                    exit()
                if tmp[j][ans%3]==0:
                    w.appendleft(j)
                    tmp[j][ans%3]=1
    print((-1))

if __name__=="__main__":
    sol()
# Original solution
from collections import deque
import sys
input = sys.stdin.readline

def sol():
    n,m=list(map(int,input().split()))
    d={}
    for i in range(m):
        u,v=list(map(int,input().split()))
        if u in d:
            d[u].append(v)
        else:
            d[u]=[v]
    s,t=list(map(int,input().split()))
    ans=chk=0
    w=deque([0,s])
    tmp=[0]*(n+1)
    ans=1
    n+=n//2
    n*=2
    while n:
        x=w.pop()
        if x==0:
            ans+=1
            n-=1
            w.appendleft(0)
        elif x in d:
            for j in d[x]:
                if tmp[j]==0 or tmp[j]%3!=ans%3:
                    w.appendleft(j)
                    tmp[j]=ans
        if ans%3==0 and tmp[t]==ans:
            print((ans//3))
            exit()
    print((-1))
 
if __name__=="__main__":
    sol()

# Optimized solution
from collections import deque
import sys
input = sys.stdin.readline

def sol():
    n,m=list(map(int,input().split()))
    d={}
    for i in range(m):
        u,v=list(map(int,input().split()))
        if u in d:
            d[u].append(v)
        else:
            d[u]=[v]
    s,t=list(map(int,input().split()))
    ans=0
    w=deque([0,s])
    tmp=[[0,0,0] for i in range(n+1)]
    ans=1
    n*=3
    while n:
        x=w.pop()
        if x==0:
            ans+=1
            n-=1
            w.appendleft(0)
        elif x in d:
            for j in d[x]:
                if ans%3==0 and j==t:
                    print((ans//3))
                    exit()
                if tmp[j][ans%3]==0:
                    w.appendleft(j)
                    tmp[j][ans%3]=1
    print((-1))

if __name__=="__main__":
    sol()
# Original solution
from collections import deque
import sys
input = sys.stdin.readline
def MI():return list(map(int,input().split()))
def main():
  n,m=MI()
  G=[[] for _ in range(n)]
  for _ in range(m):
    u,v=MI()
    u-=1
    v-=1
    G[u].append(v)
  s,t=MI()
  s-=1
  t-=1
  
  fi=[True]*n
  se=[True]*n
  th=[True]*n
  th[s]=False
  
  dq=deque([])
  dq.append(s)
  depth=0
  
  while dq:
    depth+=1
    tank1=[]
    tank2=[]
    tank3=[]
    
    for p in dq:
      for c in G[p]:
        if fi[c]:
          fi[c]=False
          tank2.append(c)
    for p in tank2:
      for c in G[p]:
        if se[c]:
          se[c]=False
          tank3.append(c)
    for p in tank3:
      for c in G[p]:
        if th[c]:
          th[c]=False
          tank1.append(c)
        if c==t:
          print(depth)
          exit()
    dq=deque(tank1)
  print((-1))  
if __name__=='__main__':
  main()


# Optimized solution
#from collections import deque
import sys
input = sys.stdin.readline
def MI():return list(map(int,input().split()))
def main():
  n,m=MI()
  G=[[] for _ in range(n)]
  for _ in range(m):
    u,v=MI()
    u-=1
    v-=1
    G[u].append(v)
  s,t=MI()
  s-=1
  t-=1
  
  fi=[True]*n
  se=[True]*n
  th=[True]*n
  th[s]=False
  
  #dq=deque([])
  #dq.append(s)
  dq=[s]
  depth=0
  
  while dq:
    depth+=1
    tank1=[]
    tank2=[]
    tank3=[]
    
    for p in dq:
      for c in G[p]:
        if fi[c]:
          fi[c]=False
          tank2.append(c)
    for p in tank2:
      for c in G[p]:
        if se[c]:
          se[c]=False
          tank3.append(c)
    for p in tank3:
      for c in G[p]:
        if th[c]:
          th[c]=False
          tank1.append(c)
        if c==t:
          print(depth)
          exit()
    dq=tank1
  print((-1))  
if __name__=='__main__':
  main()
# Original solution
import sys
#色々とメモリ使用量には気をつける
from collections import deque

n, m=list(map(int, input().split()))
u_list=[]
v_list=[]
for i in range(m):
    u, v=list(map(int, input().split()))
    u_list.append(u)
    v_list.append(v)
s, t=list(map(int, input().split()))
queue=deque()
kenken=[]
for i in range(1, n+1):
    queue.append((i, 0))
    while(len(queue))!=0:
        target=queue.popleft()
        target_pos=target[0]
        target_val=target[1]
        for j in range(m):
            if u_list[j]==target_pos:
                if target_val+1==3:
                    kenken.append((i, v_list[j]))
                else:
                    queue.append((v_list[j], target_val+1))

use_edge=[1 for i in range(len(kenken))]

queue.append((s, 0))
while(len(queue)!=0):
    target=queue.popleft()
    target_pos=target[0]
    target_val=target[1]
    for j in range(m):
        if use_edge[j]:

            if kenken[j][0]==target_pos:
                if kenken[j][1]==t:
                    print((target_val+1))
                    sys.exit()
                else:
                    queue.append((kenken[j][1], target_val+1))
                    use_edge[j]=0
print((-1))


# Optimized solution
import sys
from collections import deque
n, m=list(map(int,input().split()))
"""
頂点1~nを1~3nに3倍加する
"""
edges=[[] for i in range(3*n)]
for i in range(m):
    """
    uからvへの枝を
    uからv+n, u+nからv+2n, u+2nからv+3n
    とする
    (枝を3倍加)
    """
    u, v=list(map(int, input().split()))
    u, v=u-1, v-1
    edges[u].append(v+n)
    edges[u+n].append(v+2*n)
    edges[u+2*n].append(v)
s, t=list(map(int, input().split()))
s, t=s-1, t-1
q=deque()
q.append(s)
dist=[-1]*(3*n)
dist[s]=0
"""
結局求めるものは，s~tへの最短距離を3で割ったもの
"""
while len(q)!=0:
    fr=q.popleft()
    for to in edges[fr]:
        #もし行ったことがなければ
        if dist[to]==-1:
            dist[to]=dist[fr]+1
            q.append(to)
print((dist[t]//3))
# Original solution
#132_E

import queue
import sys
sys.setrecursionlimit(300000)
 
n,m=list(map(int,input().split()))

edges=[[] for i in range(n)]
for _ in range(m):
    u,v=list(map(int,input().split()))
    edges[u-1].append(v-1)
edges_3=[[] for i in range(n)]
for i in range(n):
    for x in edges[i]:
        for y in edges[x]:
            for z in edges[y]:
                edges_3[i].append(z)
    
s,t=list(map(int,input().split()))
s,t=s-1,t-1
    
def bfs(s,t):
    que=queue.Queue()
    que.put(s)
    d[s]=0
    visit[s]=True
    while not que.empty():
        q=que.get()
        if q==t:
            return d[t]
        for z in edges_3[q]:
            if visit[z]==False:
                que.put(z)
                d[z]=d[q]+1
                visit[z]=True
    return d[t]
                
visit=[False for _ in range(n)]
d=[-1 for _ in range(n)]

print((bfs(s,t)))

# Optimized solution
#### 132_E

import queue
import sys
sys.setrecursionlimit(300000)
 
n,m=list(map(int,input().split()))

edges=[[] for i in range(3*n)]
for _ in range(m):
    u,v=list(map(int,input().split()))
    u,v=u-1,v-1
    u0,u1,u2=u,u+n,u+2*n
    v0,v1,v2=v,v+n,v+2*n
    edges[u0].append(v1)
    edges[u1].append(v2)
    edges[u2].append(v0)
    
s,t=list(map(int,input().split()))
s,t=s-1,t-1
    
def bfs(s,t):
    que=queue.Queue()
    que.put(s)
    d[s]=0
    visit[s]=True
    while not que.empty():
        q=que.get()
        if q==t:
            return d[t]
        for z in edges[q]:
            if visit[z]==False:
                que.put(z)
                d[z]=d[q]+1
                visit[z]=True
    return d[t]
                
visit=[False for _ in range(3*n)]
d=[-1 for _ in range(3*n)]

print((bfs(s,t)//3))
# Original solution
from collections import defaultdict as dd
from collections import deque
from copy import deepcopy as dc
N, M = list(map(int, input().split()))
e = dd(list)
inf = 10 ** 9
for _ in range(M):
  u, v = list(map(int, input().split()))
  e[u].append(v)
S, T = list(map(int, input().split()))

stopped = [inf] * (N + 1)
Q = deque()
Q.append(S)
stopped[S] = 0
saw2 = {i: 0 for i in range(1, N + 1)}
#saw3 = {i: 0 for i in range(1, N + 1)}
while len(Q) != 0:
  s2 = dc(saw2)
  #s3 = dc(saw3)
  p = Q.popleft()
  if stopped[p] >= stopped[T]:
    break
  for q in e[p]:
    for r in e[q]:
      if s2[r] == 1:
        continue
      else:
        s2[r] = 1
      for s in e[r]:
        #if s3[s] == 1:
        #  continue
        #else:
        #  s3[s] = 1
        if stopped[s] > stopped[p] + 1:
          Q.append(s)
          stopped[s] = stopped[p] + 1
if stopped[T] == inf:
  print((-1))
else:
  print((stopped[T]))

# Optimized solution
from collections import defaultdict as dd
from collections import deque
N, M = list(map(int, input().split()))
e = dd(list)
inf = 10 ** 9
for _ in range(M):
  u, v = list(map(int, input().split()))
  e[u].append(v + N)
  e[u + N].append(v + 2 * N)
  e[u + 2 * N].append(v)
S, T = list(map(int, input().split()))
S += 2 * N
T += 2 * N
stopped = [inf] * (3 * (N + 1))
Q = deque()
Q.append(S)
stopped[S] = 0
while len(Q) != 0:
  p = Q.popleft()
  if stopped[p] >= stopped[T]:
    break
  for q in e[p]:
    if stopped[q] > stopped[p] + 1:
      Q.append(q)
      stopped[q] = stopped[p] + 1
if stopped[T] == inf:
  print((-1))
else:
  print((stopped[T] // 3))
# Original solution
from collections import deque
n,m = list(map(int,input().split()))
path = [[set() for _ in range(n)] for _ in range(3)]
for _ in range(m):
    u,v = list(map(int,input().split()))
    u -= 1
    v -= 1
    path[0][u].add(v)

s,t = list(map(int,input().split()))
for i in range(2):
    for j in range(n):
        for k in path[i][j]:
            path[i+1][j] = path[i+1][j] | path[0][k]
que = deque([s-1])
d = [float('inf')]*n
d[s-1] = 0
while que:
    p = que.popleft()
    for to in path[2][p]:
        if d[to] == float('inf'):
            d[to] = d[p] + 1
            que.append(to)
if d[t-1] == float('inf'):
    d[t-1] = -1
print((d[t-1]))


# Optimized solution
from collections import deque
n,m = list(map(int,input().split()))
path = [set() for _ in range(n)]
for _ in range(m):
    u,v = list(map(int,input().split()))
    u -= 1
    v -= 1
    path[u].add(v)

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

que = deque([(s-1,0)])
d = [[float('inf')]*n for _ in range(3)]
d[0][s-1] = 0
while que:
    p,i = que.popleft()
    j = (i+1)%3
    for to in path[p]:
        if d[j][to] == float('inf'):
            d[j][to] = d[i][p] + 1
            que.append((to,j))
if d[0][t-1] == float('inf'):
    d[0][t-1] = -3
print((d[0][t-1]//3))
# Original solution
from collections import deque


def main():
    N, M = list(map(int, input().split()))
    dp = [[set() for _ in range(N)] for _ in range(3)]
    for _ in range(M):
        u, v = list(map(int, input().split()))
        dp[0][v-1].add(u-1)
    S, T = list(map(int, input().split()))
    for i in range(1, 3):
        for j in range(N):
            tmp = set()
            for nxt in dp[i-1][j]:
                tmp = tmp.union(dp[0][nxt])
            dp[i][j] = tmp
    adj = [set() for _ in range(N)]
    for i in range(N):
        for j in dp[2][i]:
            adj[j].add(i)
    searchq = deque()
    visited = set()
    searchq.append((S-1, 0))
    visited.add(S-1)
    while len(searchq):
        p, cost = searchq.popleft()
        if p == T-1:
            print(cost)
            return
        for nxt in adj[p]:
            if nxt not in visited:
                searchq.append((nxt, cost+1))
                visited.add(nxt)
    print((-1))


if __name__ == "__main__":
    main()


# Optimized solution
from collections import deque


def main():
    N, M = list(map(int, input().split()))
    dp = [[[] for _ in range(N)] for _ in range(3)]
    for _ in range(M):
        u, v = list(map(int, input().split()))
        dp[0][v-1].append(u-1)
    S, T = list(map(int, input().split()))
    for i in range(1, 3):
        for j in range(N):
            tmp = list()
            for nxt in dp[i-1][j]:
                tmp += dp[0][nxt]
            dp[i][j] = set(tmp)
    adj = [set() for _ in range(N)]
    for i in range(N):
        for j in dp[2][i]:
            adj[j].add(i)
    searchq = deque()
    visited = set()
    searchq.append((S-1, 0))
    visited.add(S-1)
    while len(searchq):
        p, cost = searchq.popleft()
        if p == T-1:
            print(cost)
            return
        for nxt in adj[p]:
            if nxt not in visited:
                searchq.append((nxt, cost+1))
                visited.add(nxt)
    print((-1))


if __name__ == "__main__":
    main()
# Original solution
from collections import deque


def main():
    N, M = list(map(int, input().split()))
    dp = [[[] for _ in range(N)] for _ in range(3)]
    for _ in range(M):
        u, v = list(map(int, input().split()))
        dp[0][v-1].append(u-1)
    S, T = list(map(int, input().split()))
    for i in range(1, 3):
        for j in range(N):
            tmp = list()
            for nxt in dp[i-1][j]:
                tmp += dp[0][nxt]
            dp[i][j] = set(tmp)
    adj = [set() for _ in range(N)]
    for i in range(N):
        for j in dp[2][i]:
            adj[j].add(i)
    searchq = deque()
    visited = set()
    searchq.append((S-1, 0))
    visited.add(S-1)
    while len(searchq):
        p, cost = searchq.popleft()
        if p == T-1:
            print(cost)
            return
        for nxt in adj[p]:
            if nxt not in visited:
                searchq.append((nxt, cost+1))
                visited.add(nxt)
    print((-1))


if __name__ == "__main__":
    main()


# Optimized solution
from collections import deque


def main():
    N, M = list(map(int, input().split()))
    dp = [[[] for _ in range(N)] for _ in range(3)]
    for _ in range(M):
        u, v = list(map(int, input().split()))
        dp[0][v-1].append(u-1)
    S, T = list(map(int, input().split()))
    for i in range(1, 3):
        for j in range(N):
            tmp = list()
            for nxt in dp[i-1][j]:
                tmp += dp[0][nxt]
            if len(tmp):
                dp[i][j] = list(set(tmp))
    adj = [set() for _ in range(N)]
    for i in range(N):
        for j in dp[2][i]:
            adj[j].add(i)
    searchq = deque()
    visited = set()
    searchq.append((S-1, 0))
    visited.add(S-1)
    while len(searchq):
        p, cost = searchq.popleft()
        if p == T-1:
            print(cost)
            return
        for nxt in adj[p]:
            if nxt not in visited:
                searchq.append((nxt, cost+1))
                visited.add(nxt)
    print((-1))


if __name__ == "__main__":
    main()
# Original solution
from collections import deque


def main():
    N, M = list(map(int, input().split()))
    dp = [[[] for _ in range(N)] for _ in range(3)]
    for _ in range(M):
        u, v = list(map(int, input().split()))
        dp[0][v-1].append(u-1)
    S, T = list(map(int, input().split()))
    for i in range(1, 3):
        for j in range(N):
            tmp = list()
            for nxt in dp[i-1][j]:
                tmp += dp[0][nxt]
            if len(tmp):
                dp[i][j] = list(set(tmp))
    adj = [set() for _ in range(N)]
    for i in range(N):
        for j in dp[2][i]:
            adj[j].add(i)
    searchq = deque()
    visited = set()
    searchq.append((S-1, 0))
    visited.add(S-1)
    while len(searchq):
        p, cost = searchq.popleft()
        if p == T-1:
            print(cost)
            return
        for nxt in adj[p]:
            if nxt not in visited:
                searchq.append((nxt, cost+1))
                visited.add(nxt)
    print((-1))


if __name__ == "__main__":
    main()


# Optimized solution
from collections import deque


def main():
    N, M = list(map(int, input().split()))
    adj = [set() for _ in range(N*3)]
    dp = [[[] for _ in range(N)] for _ in range(3)]
    for _ in range(M):
        u, v = list(map(int, input().split()))
        u -= 1
        v -= 1
        adj[u].add(N*1+v)
        adj[N*1+u].add(N*2+v)
        adj[N*2+u].add(v)
    S, T = list(map(int, input().split()))
    searchq = deque()
    visited = set()
    searchq.append((S-1, 0))
    visited.add(S-1)
    while len(searchq):
        p, cost = searchq.popleft()
        if p == T-1:
            print((cost//3))
            return
        for nxt in adj[p]:
            if nxt not in visited:
                searchq.append((nxt, cost+1))
                visited.add(nxt)
    print((-1))


if __name__ == "__main__":
    main()
# Original solution
def main():
	N, M = list(map(int, input().split()))
	INF = 2**30-1
	mat = [[INF for _ in range(3*N)] for _ in range(3*N)]
	for _ in range(M):
		u, v = list(map(int, input().split()))
		mat[u-1][N+v-1] = 1
		mat[N+u-1][2*N+v-1] = 1
		mat[2*N+u-1][v-1] = 1
	for i in range(3*N):
		for j in range(3*N):
			for k in range(3*N):
				mat[j][k] = min(mat[j][k], mat[j][i]+mat[i][k])

	S, T = list(map(int, input().split()))
	if mat[S-1][T-1] == INF:
		print((-1))
	else:
		print((mat[S-1][T-1]//3))


if __name__ == '__main__':
	main()

# Optimized solution
def main():
	N, M = list(map(int, input().split()))
	INF = 2**30-1
	mat = [[] for _ in range(3*N)]
	for _ in range(M):
		u, v = list(map(int, input().split()))
		mat[u-1].append([N+v-1, 1])
		mat[N+u-1].append([2*N+v-1, 1])
		mat[2*N+u-1].append([v-1, 1])

	S, T = list(map(int, input().split()))
	queue = list(mat[S-1])
	visited = [False]*(3*N)
	visited[S-1] = True
	while queue:
		pos, d = queue.pop()
		if pos == T-1:
			print((d//3))
			return
		visited[pos] = True
		next_positions = mat[pos]
		for next_pos, next_d in next_positions:
			if visited[next_pos]:
				continue
			queue.append([next_pos, d+next_d])
		queue.sort(key=lambda x: x[1])
	print((-1))

if __name__ == '__main__':
	main()
# Original solution
from collections import deque
n, m = list(map(int, input().split()))

g = [[] 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].append(v)
# print(g)

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

visited = [[-1]*3 for j in range(n)]

l = 0
queue = deque([[s, l]])
visited[s][l] = 0
while queue:
  x, l = queue.popleft()
  if [x, l] == [t, 0]:
    print((visited[x][l]//3))
    exit()
  for x_ in g[x]:
    new_x, new_l = x_, (l+1)%3
    if visited[new_x][new_l] == -1:
      visited[new_x][new_l] = visited[x][l]+1
      queue.append([new_x, new_l])
    
print((visited[t][0]))

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

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

from collections import deque
q = deque()
visit = [-1]*(3*n)
q.append(s)
visit[s] = 0
while q:
    u = q.popleft()
    for v in g[u]:
        if visit[v] == -1:
            visit[v] = visit[u]+1
            q.append(v)
if visit[t] == -1:
    print((-1))
else:
    print((visit[t]//3))