CodeEff/ECCO huggingface dataset:
# Original solution
import queue

n,m = list(map(int, input().split()))
a = [[] for i in range(n)]

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

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

inf = float('inf')
d = [[inf, inf, inf] for i in range(n)]
d[s][0]=0

q = queue.Queue()
for i in range(len(a[s])):
    q.put(a[s][i])
    d[a[s][i]][1]=1


while not q.empty():
    p = q.get()
    for i in range(len(a[p])):
        r = a[p][i]
        c = []
        if d[r][0]==inf:
            c.append(0)
        if d[r][1]==inf:
            c.append(1)
        if d[r][2]==inf:
            c.append(2)
        if not c:
            continue

        flag = False
        for j in range(len(c)):
            if c[j]==0:
                if d[p][2]!=inf:
                    break
            elif c[j]==1:
                if d[p][0]!=inf:
                    break
            elif c[j]==2:
                if d[p][1]!=inf:
                    break
            if j==len(c)-1:
                flag=True
        if flag:
            continue

        q.put(r)
        if d[r][0]==inf:
            d[r][0]=d[p][2]+1
        if d[r][1]==inf:
            d[r][1]=d[p][0]+1
        if d[r][2]==inf:
            d[r][2]=d[p][1]+1
        if r==t and d[r][0]!=inf:
            print((d[r][0]//3))
            exit()
print((-1))






# Optimized solution
import queue

n,m = list(map(int, input().split()))
a = [[] for i in range(n)]

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

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

inf = float('inf')
d = [[inf, inf, inf] for i in range(n)]
d[s][0]=0

q = queue.Queue()
for i in range(len(a[s])):
    q.put(a[s][i])
    d[a[s][i]][1]=1


while not q.empty():
    p = q.get()
    for i in range(len(a[p])):
        r = a[p][i]
        c = []
        if d[r][0]==inf:
            c.append(0)
        if d[r][1]==inf:
            c.append(1)
        if d[r][2]==inf:
            c.append(2)
        if not c:
            continue

        flag = False
        for j in range(len(c)):
            if c[j]==0:
                if d[p][2]!=inf:
                    break
            elif c[j]==1:
                if d[p][0]!=inf:
                    break
            elif c[j]==2:
                if d[p][1]!=inf:
                    break
            if j==len(c)-1:
                flag=True
        if flag:
            continue

        q.put(r)
        for j in range(len(c)):
            if c[j]==0:
                d[r][0] = d[p][2] + 1
            elif c[j]==1:
                d[r][1] = d[p][0] + 1
            elif c[j]==2:
                d[r][2] = d[p][1] + 1

        if r==t and d[r][0]!=inf:
            print((d[r][0]//3))
            exit()
print((-1))
# Original solution
import sys
import heapq
n, m = [int(i) for i in sys.stdin.readline().split()]
graph = [[0 for i in range(3 * n)] for j in range(3 * n)]
for i in range(m):
    u, v = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    graph[u][n+v] = 1
    graph[u+n][2*n+v] = 1
    graph[u + 2 * n][v] = 1
s, t = [int(i) for i in sys.stdin.readline().split()]
s -= 1
t -= 1

que = [(0,s)]
already = set()
flg = False
while que:
    cost, cur = heapq.heappop(que)
    already.add(cur)
    if cur == t:
        flg = True
        break
    for i, _next in enumerate(graph[cur]):
        if _next == 0:
            continue
        if i in already:
            continue
        heapq.heappush(que, (cost + 1, i))
if not flg:
    cost = -1
else:
    cost //= 3

print(cost)

# Optimized solution
import sys
import heapq
n, m = [int(i) for i in sys.stdin.readline().split()]
graph = {j:[] for j in range(3 * n)}
for i in range(m):
    u, v = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    graph[u].append(n+v)
    graph[u+n].append(2*n+v)
    graph[u + 2 * n].append(v)
s, t = [int(i) for i in sys.stdin.readline().split()]
s -= 1
t -= 1

que = [(0,s)]
already = set()
flg = False
while que:
    cost, cur = heapq.heappop(que)
    already.add(cur)
    if cur == t:
        flg = True
        break
    for i in graph[cur]:
        if i in already:
            continue
        heapq.heappush(que, (cost + 1, i))
if not flg:
    cost = -1
else:
    cost //= 3

print(cost)
# Original solution
import sys
from collections import deque
n, m = [int(i) for i in sys.stdin.readline().split()]
graph = {j:[] for j in range(3 * n)}
for i in range(m):
    u, v = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    graph[u].append(n+v)
    graph[u+n].append(2*n+v)
    graph[u + 2 * n].append(v)
s, t = [int(i) for i in sys.stdin.readline().split()]
s -= 1
t -= 1
 
que = deque([(0, s)])
already = set()
flg = False
while que:
    cost, cur = que.popleft()
    already.add(cur)
    if cur == t:
        flg = True
        break
    for i in graph[cur]:
        if i not in already:
            que.append((cost + 1, i))
if not flg:
    cost = -1
else:
    cost //= 3
print(cost)

# Optimized solution
import sys
INF = 10 ** 9
n, m = [int(i) for i in sys.stdin.readline().split()]
graph = {j:[] for j in range(3 * n)}
for i in range(m):
    u, v = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    graph[u].append(n+v)
    graph[u+n].append(2*n+v)
    graph[u + 2 * n].append(v)
s, t = [int(i) for i in sys.stdin.readline().split()]
s -= 1
t -= 1

que = [s]
flg = False
already = set()
dist = [INF for i in range(3 * n)]
dist[s] = 0
while que:
    cur= que[0]
    que.pop(0)
    for i in graph[cur]:
        if i not in already:
            if i == t:
                res = dist[cur] + 1
                que.clear()
                flg = True
                break
            que.append(i)
            dist[i] = dist[cur] + 1
            already.add(i)
if not flg:
    res = -1
else:
    res //= 3

print(res)
# Original solution
import queue

N,M = list(map(int, input().split()))
G = [[] for i in range(3*N+1)]

for i in range(M):
  u,v = list(map(int, input().split()))
  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()))

def bfs(s):
  que = queue.Queue()
  que.put(s)
  d = [-1 for _ in range(3*N+1)]
  d[s] = 0

  while not que.empty():

    p = que.get()
    if (T == p):
      return d[p]

    for pp in G[p]:
      if d[pp] == -1:
        que.put(pp)
        d[pp] = d[p]+1

  return -1

ans = bfs(S)
if (-1 == ans):
  print((-1))
else:
  print((ans//3))


# Optimized solution
import collections

N,M = list(map(int, input().split()))
G = [[] for i in range(3*N+1)]

for i in range(M):
  u,v = list(map(int, input().split()))
  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()))

def bfs(s):
  que = collections.deque()
  que.append(s)
  d = [-1 for _ in range(3*N+1)]
  d[s] = 0

  while (0 < len(que)):

    p = que.popleft()
    if (T == p):
      return d[p]

    for pp in G[p]:
      if d[pp] == -1:
        que.append(pp)
        d[pp] = d[p]+1

  return -1

ans = bfs(S)
if (-1 == ans):
  print((-1))
else:
  print((ans//3))
# Original solution
from collections import deque


def nearlist(N, LIST):  # 隣接リスト
    NEAR = [set() for _ in range(3 * N)]
    for a, b in LIST:
        a, b = 3 * (a - 1), 3 * (b - 1)
        NEAR[a].add(b + 1)
        NEAR[a + 1].add(b + 2)
        NEAR[a + 2].add(b)
    return NEAR


def bfs(S, T, N):  # 幅優先探索  # キュー
    S, T = 3 * (S - 1), 3 * (T - 1)
    dist = [-3] * (3 * N)  # 前処理
    dist[S] = 0
    que, flag = deque([S]), set([S])

    while que:
        q = que.popleft()
        for i in near[q]:  # 移動先の候補
            if i in flag:  # 処理済みか否か
                continue
            dist[i] = dist[q] + 1
            que.append(i), flag.add(i)
    return dist[T] // 3


n, m = list(map(int, input().split()))
uv = [list(map(int, input().split())) for _ in range(m)]
s, t = list(map(int, input().split()))

# 解説AC
near = nearlist(n, uv)
ans = bfs(s, t, n)
print(ans)


# Optimized solution
from collections import deque


def nearlist(N, LIST):  # 隣接リスト
    NEAR = [set() for _ in range(3 * N)]
    for a, b in LIST:
        a, b = 3 * (a - 1), 3 * (b - 1)
        NEAR[a].add(b + 1)
        NEAR[a + 1].add(b + 2)
        NEAR[a + 2].add(b)
    return NEAR


def bfs(S, T, N):  # 幅優先探索  # キュー
    S, T = 3 * (S - 1), 3 * (T - 1)
    dist, flag = [-3] * (3 * N), [0] * (3 * N)  # 前処理
    dist[S], flag[S], que = 0, 1, deque([S])

    while que:
        q = que.popleft()
        for i in near[q]:  # 移動先の候補
            if flag[i]:  # 処理済みか否か
                continue
            dist[i] = dist[q] + 1
            flag[i] = 1
            que.append(i)
    return dist[T] // 3


n, m = list(map(int, input().split()))
uv = [list(map(int, input().split())) for _ in range(m)]
s, t = list(map(int, input().split()))

# 解説AC
near = nearlist(n, uv)
ans = bfs(s, t, n)
print(ans)
# Original solution
from collections import deque
N, M = list(map(int, input().split()))
G = {k: [] for k 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 = [[10**9] * (N+1) for _ in range(3)]


que = deque([(S, 0)])

while que:
    ci, t = que.popleft()
    visited[t % 3][ci] = t

    for ni in G[ci]:
        if visited[(t+1) % 3][ni] > t+1:
            que.append((ni, t+1))

if visited[0][T] == 10**9:
    print((-1))
else:
    print((visited[0][T]//3))


# Optimized solution
from collections import deque
N, M = list(map(int, input().split()))
G = [[[] for j in range(3)] for i in range(N+1)]
for _ in range(M):
    u, v = list(map(int, input().split()))
    for i in range(3):
        G[u][i].append((v, (i+1) % 3))
S, T = list(map(int, input().split()))


visited = [[-1] * 3 for _ in range(N+1)]

que = deque([(S, 0, 0)])
visited[S][0] = 0

while que:
    ci, l, d = que.popleft()

    for ni, nl in G[ci][l]:
        if visited[ni][nl] == -1:
            que.append((ni, nl, d+1))
            visited[ni][nl] = d+1

print((visited[T][0]//3))
# Original solution
from collections import deque
n,m=list(map(int,input().split()))
g=[[] for i in range(n)]
for i in range(m):
    a,b=list(map(int,input().split()))
    g[a-1].append(b-1)
gken=[[] for i in range(n)]
for i in range(n):
    for j in g[i]:
        for j2 in g[j]:
            for j3 in g[j2]:
                gken[i].append(j3)

s,t=list(map(int,input().split()))
s-=1
t-=1
q=deque([[s,0]])
v=[0]*n
while q:
    p=q.popleft()
    v[p[0]]=1
    for j3 in gken[p[0]]:
        if j3==t:
            print((p[1]+1))
            exit()
        if v[j3]==0:
            v[j3]=1
            q.append([j3,p[1]+1])
print((-1))


# Optimized solution
from collections import deque
n,m=list(map(int,input().split()))
g=[[] for i in range(3*n)]
for i in range(m):
    a,b=list(map(int,input().split()))
    g[3*(a-1)].append(3*(b-1)+1)
    g[3*(a-1)+1].append(3*(b-1)+2)
    g[3*(a-1)+2].append(3*(b-1))
s,t=list(map(int,input().split()))
s-=1
t-=1
s*=3
t*=3
q=deque([[s,0]])
v=[0]*(3*n)
while q:
    p=q.popleft()
    v[p[0]]=1
    for j3 in g[p[0]]:
        if j3==t:
            print(((p[1]+1)//3))
            exit()
        if v[j3]==0:
            v[j3]=1
            q.append([j3,p[1]+1])
print((-1))
# Original solution
from collections import deque
n,m = list(map(int,input().split()))

uv = [[] for i in range(n)]

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

check = [[0 for i in range(3)] for j in range(n)]

d = deque()
d.appendleft([0,s-1])
#check[s-1][0] = 1

while len(d) > 0:
    for i in range(len(d)):
        num,ver = d.popleft()
        check[ver][num%3] = 1
        num = num+1
        #cnt += 1
        for j in range(len(uv[ver])):
            #print(cnt,num,ver,j,t-1,uv[ver][j])
            if uv[ver][j] == t-1 and num%3 == 0:
                print((num//3))
                exit()
            if check[uv[ver][j]][num%3] == 0:
                #check[uv[ver][j]][num] = 1
                d.append([num,uv[ver][j]])
                
print((-1))

# Optimized solution
# coding: utf-8
import sys
from heapq import heapify, heappop, heappush

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# vertexごとに3つの状態数を持つ
N, M = lr()
graph = [[] for _ in range((N+1)*3)]  # 1-indexed
for _ in range(M):
    a, b = lr()
    a *= 3; b *= 3
    for i in range(3):
        j = i+1 if i < 2 else 0
        graph[a+i].append(b+j)

INF = 10 ** 15

def dijkstra(start):
    dist = [INF] * (N+1) * 3
    dist[start] = 0
    que = [(0, start)]
    while que:
        d, prev = heappop(que)
        if dist[prev] < d:
            continue
        for i in graph[prev]:
            #print(graph[prev],dist[next],next)
            d1 = d + 1
            if dist[i] > d1:
                dist[i] = d1
                heappush(que, (d1, i))
    return dist

S, T = lr()
dist = dijkstra(S*3)
answer = dist[T*3]
if answer == INF:
    answer = -1
else:
    answer //= 3
print(answer)
# Original solution
import copy
a,b=list(map(int,input().split()))
c=[list(map(int,input().split())) for i in range(b)]
s,t=list(map(int,input().split()))
s=3*s
t=3*t
dd=[[]for i in range(a+1)]
for i in range(b):
  da=c[i][0]
  db=c[i][1]
  dd[da].append(db)
  

d=[[]for i in range(3*(a+1))]
for i in range(len(dd)):
  for j in dd[i]:
      d[3*i].append(3*j+1)
      d[3*i+1].append(3*j+2)
      d[3*i+2].append(3*j)
e=[s]
f=[s]
h=0
for i in range(1,3*a+3):
  g=[]
  for j in f:
    for k in d[j]:
      if k not in e:
        e.append(k)
        g.append(k)
        if k==t:
          print((i//3))
          h=1
          break
    if h==1:
      break
  if h==1:
    break
  if g==[]:
    print((-1))
    break
  f=copy.copy(g)

# Optimized solution
import copy
a,b=list(map(int,input().split()))
c=[list(map(int,input().split())) for i in range(b)]
s,t=list(map(int,input().split()))
s=3*s
t=3*t
dd=[[]for i in range(a+1)]
for i in range(b):
  da=c[i][0]
  db=c[i][1]
  dd[da].append(db)
  

d=[[]for i in range(3*(a+1))]
for i in range(len(dd)):
  for j in dd[i]:
      d[3*i].append(3*j+1)
      d[3*i+1].append(3*j+2)
      d[3*i+2].append(3*j)
e=[-1 for i in range(3*a+3)]
f=[s]
h=0
for i in range(1,3*a+3):
  g=[]
  for j in f:
    for k in d[j]:
      if e[k]==-1:
        e[k]=1
        g.append(k)
        if k==t:
          print((i//3))
          h=1
          break
    if h==1:
      break
  if h==1:
    break
  if g==[]:
    print((-1))
    break
  f=copy.copy(g)
# Original solution
import sys
from heapq import heappush, heappop

inf = float('inf')

n, m = list(map(int, sys.stdin.readline().split()))
g = [[] for _ in range(n+1)]
for _ in range(m):
    u, v = list(map(int, sys.stdin.readline().split()))
    g[u].append(v)

s, t = list(map(int, sys.stdin.readline().split()))

g2 = [[] for _ in range(n + 1)]
e = set()
hq = []
for u in range(1, n+1):
    for v in g[u]:
        heappush(hq, (1, u, v))

while hq:
    dux = heappop(hq)
    if dux in e:
        continue
    e.add(dux)
    d, u, x = dux
    if d == 3:
        g2[u].append(x)
        continue
    for y in g[x]:
        heappush(hq, (d+1, u, y))

def main():
    hq = [(0, s)]
    dist = [inf] * (n + 1)
    while hq:
        d, x = heappop(hq)
        if dist[x] != inf:
            continue
        dist[x] = d
        for y in g2[x]:
            if dist[y] == inf:
                heappush(hq, (d+1, y))
    
    return dist[t] if dist[t] != inf else -1

if __name__ == '__main__':
    ans = main()
    print(ans)

# Optimized solution
import sys
from heapq import heappush,heappop

inf = float('inf')

n, m = list(map(int, sys.stdin.readline().split()))
g = [[] for _ in range(n+1)]
for _ in range(m):
    u, v = list(map(int, sys.stdin.readline().split()))
    g[u].append(v)

s, t = list(map(int, sys.stdin.readline().split()))

def main():
    dist = [inf] * (n + 1)
    can_reach = [[False] * 3 for _ in range(n + 1)]
    hq = [(0, s)]
    can_reach[s][0] = True
    while hq:
        d, x = heappop(hq)
        if d % 3 == 0:
            dist[x] = min(dist[x], d // 3)
        d += 1
        for y in g[x]:
            if not can_reach[y][d%3]:
                can_reach[y][d%3] = True
                heappush(hq, (d, y))

    return dist[t] if dist[t] != inf else -1

if __name__ == '__main__':
    ans = main()
    print(ans)
# Original solution
def BC132_E():
    n, m = list(map(int, input().split(' ')))
    e = [[] for _ in range(n)]
    for _ in range(m):
        u, v = list(map(int, input().split(' ')))
        e[u - 1].append(v - 1)

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

    f = []
    for i in range(n):
        a = set()
        for j in e[i]:
            a.update(e[j])
        b = set()
        for j in list(a):
            b.update(e[j])
        f.append(b)

    stack, visited = [(s, 0)], set()
    while stack:
        i, d = stack.pop(0)
        if i == t: return d
        if i in visited:
            continue
        visited.add(i)
        if t in f[i]:
            return d + 1
        else:
            stack += [(x, d + 1) for x in list(f[i])]
    return -1


print((BC132_E()))

# Optimized solution
def BC132_E():
    n, m = list(map(int, input().split(' ')))
    e = [[] for _ in range(n)]
    for _ in range(m):
        u, v = list(map(int, input().split(' ')))
        e[u - 1].append(v - 1)
    s, t = list(map(int, input().split(' ')))
    s, t = s - 1, t - 1
    stack, visited = [(s, 0)], set()
    a, b = set(), set()
    while stack:
        i, d = stack.pop(0)
        if i == t: return d
        if i in visited:
            continue
        visited.add(i)
        v = set()
        for j in e[i]:
            if j in a: continue
            a.add(j)
            for k in e[j]:
                if k in b: continue
                b.add(k)
                v.update(e[k])
        if t in v:
            return d + 1
        else:
            stack += [(x, d + 1) for x in list(v)]
    return -1


print((BC132_E()))
# Original solution
# import bisect
from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m = list(map(int, ipti().split()))
    node_next = [set() for _ in range(n)]

    for i in range(m):
        u, v = list(map(int, ipti().split()))
        node_next[u-1].add(v-1)

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

    seen = [[0] * n for _ in range(3)]

    que = deque()
    que.append((s, 0, 0))

    while que:
        cnode, cstate, dist = que.popleft()
        seen[cstate][cnode] = 1
        dist += 1
        nstate = (cstate + 1) % 3
        for nnode in node_next[cnode]:
            if seen[nstate][nnode] == 0:
                if nstate == 0 and nnode == t:
                    print((dist // 3))
                    sys.exit()
                else:
                    que.append((nnode, nstate, dist))

    print((-1))


if __name__ == '__main__':
    main()


# Optimized solution
# import bisect
from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m = list(map(int, ipti().split()))
    node_next = [set() for _ in range(n)]

    for i in range(m):
        u, v = list(map(int, ipti().split()))
        node_next[u - 1].add(v - 1)

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

    dist = [[INF] * n for _ in range(3)]
    dist[0][s] = 0

    que = deque()
    que.append((0, s))

    while que:
        cstate, cnode = que.popleft()
        d = dist[cstate][cnode]
        nstate = (cstate + 1) % 3
        for nnode in node_next[cnode]:
            if dist[nstate][nnode] == INF:
                dist[nstate][nnode] = d + 1
                que.append((nstate, nnode))

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


if __name__ == '__main__':
    main()
# Original solution
import heapq
N, M = list(map(int, input().split()))

edges = [set() for _ in range(N)]

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

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


step2 = [set() for _ in range(N)]

for u in range(N):
    for v in edges[u]:
        step2[u] |= edges[v]

cost = [-1 for _ in range(N)]
q = []
heapq.heappush(q, (0, S))

while len(q) != 0:
    c, u = heapq.heappop(q)
    if cost[u] != -1:
        continue
    cost[u] = c
    for u2 in step2[u]:
        rms = set()
        for v in edges[u2]:
            if cost[v] != -1:
                rms.add(v)
            else:
                heapq.heappush(q, (c+1, v))

        edges[u2] -= rms

print((cost[T]))


# Optimized solution
import heapq
N, M = list(map(int, input().split()))

edges = [set() for _ in range(N)]

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

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

cost = [-1 for _ in range(N)]
q = []
heapq.heappush(q, (0, S))

step1memo = set()
step2memo = set()
step3memo = set()

while len(q) != 0:
    c, u = heapq.heappop(q)
    if cost[u] != -1:
        continue
    cost[u] = c
    for u1 in edges[u]:
        if u1 in step1memo:
            continue
        step1memo.add(u1)
        for u2 in edges[u1]:
            if u2 in step2memo:
                continue
            step2memo.add(u2)
            for u3 in edges[u2]:
                heapq.heappush(q, (c+1, u3))

print((cost[T]))
# Original solution
N,M = list(map(int, input().split()))
UV = [list(map(int, input().split())) for _ in range(M)]
S,T = list(map(int, input().split()))

adjucent_dict={u:set() for u in range(1,N+1)}
for u,v in UV:
  adjucent_dict.setdefault(u,set()).add(v)
  #adjucent_dict.setdefault(v,set()).add(u)
  
two_adjucent_dict={u:set.union(*[adjucent_dict[v] for v in adjucent_dict[u]]) if adjucent_dict[u] else set() for u in range(1,N+1)}
three_adjucent_dict={u:set.union(*[adjucent_dict[v] for v in two_adjucent_dict[u]]) if two_adjucent_dict[u] else set() for u in range(1,N+1)}

visited=set([S])
just_visited=set([S])
n=1
while True:
  three_step = set.union(*[three_adjucent_dict[u] for u in just_visited])
  just_visited = three_step - visited
  if T in just_visited:
    print(n)
    break
  if len(just_visited)==0:
    print((-1))
    break
    
  visited = visited & just_visited
  n+=1

# Optimized solution
N,M = list(map(int, input().split()))
UV = [list(map(int, input().split())) for _ in range(M)]
S,T = list(map(int, input().split()))

adjacent_dict={}
for u,v in UV:
  adjacent_dict.setdefault((u,0),set()).add((v,1))
  adjacent_dict.setdefault((u,1),set()).add((v,2))
  adjacent_dict.setdefault((u,2),set()).add((v,0))
  
#BFS
to_visit = set([(S,0)])
visited = set()
n=1

while to_visit:
  #print(to_visit, visited, n)
  _next = set()
  for v in to_visit:
    if v in adjacent_dict:
      _next = _next.union(adjacent_dict[v])
  _next = _next - visited
  if (T,0) in _next:
    print((n//3))
    break
  to_visit = _next
  visited = visited | _next
  n += 1
  #print(to_visit, visited, n)
    
else:
  print((-1))