CodeEff/ECCO huggingface dataset:
# Original solution
import sys
import heapq
import math

input = sys.stdin.readline
N, M = list(map(int, input().split()))
uv = [list(map(int, input().split())) for _ in range(M)]
S, T = list(map(int, input().split()))

g = [[] for _ in range(3*N)]
for a, b in uv:
    g[a-1].append(b-1+N)
    g[a-1+N].append(b-1+2*N)
    g[a - 1 + 2 * N].append(b - 1)

# bfsでよかった
hq = [(0, S-1)]
costs = [math.inf] * 3 * N
while len(hq) > 0:
    c, u = heapq.heappop(hq)
    if u == T - 1:
        print((c // 3))
        sys.exit()
    costs[u] = c
    for v in g[u]:
        if costs[v] > c+1:
            heapq.heappush(hq, (c + 1, v))
print((-1))


# Optimized solution
import sys
import heapq
import math

input = sys.stdin.readline
N, M = list(map(int, input().split()))
uv = [list(map(int, input().split())) for _ in range(M)]
S, T = list(map(int, input().split()))

g = [[] for _ in range(3*N)]
for a, b in uv:
    g[a-1].append(b-1+N)
    g[a-1+N].append(b-1+2*N)
    g[a - 1 + 2 * N].append(b - 1)

# bfsでよかった
hq = [(0, S-1)]
costs = [math.inf] * 3 * N
while len(hq) > 0:
    c, u = heapq.heappop(hq)
    if u == T - 1:
        print((c // 3))
        sys.exit()
    costs[u] = c
    for v in g[u]:
        if costs[v] > c+1:
            heapq.heappush(hq, (c + 1, v))
        costs[v] = min(costs[v], c+1)

print((-1))
# Original solution
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: list(map(int, sys.stdin.readline().split()))
na = lambda: list(map(int, sys.stdin.readline().split()))
nb = lambda: list([int(x) - 1 for x in sys.stdin.readline().split()])


# ===CODE===


def main():
    n, m = ns()
    e = [[] for _ in range(n)]
    for _ in range(m):
        a, b = ns()
        a, b = a - 1, b - 1
        e[a].append(b)

    s, t = ns()
    s, t = s - 1, t - 1

    ans = -1
    que = collections.deque(e[s])
    cnt = 0
    finflg = True
    while len(que) and cnt < 3*10 ** 5 and finflg:
        cnt += 1
        finflg = True

        for _ in range(3):
            for i in range(len(que)):
                nxt = que.popleft()
                if _ == 2 and nxt == t:
                    ans = cnt
                    finflg = False
                for tmp in e[nxt]:
                    que.append(tmp)

    print((-1 if ans == -1 else ans))


if __name__ == '__main__':
    main()


# Optimized solution
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: list(map(int, sys.stdin.readline().split()))
na = lambda: list(map(int, sys.stdin.readline().split()))
nb = lambda: list([int(x) - 1 for x in sys.stdin.readline().split()])


# ===CODE===


def main():
    n, m = ns()
    e = [[] for _ in range(n)]
    for _ in range(m):
        a, b = ns()
        a, b = a - 1, b - 1
        e[a].append(b)

    s, t = ns()
    s, t = s - 1, t - 1

    e3 = [[] for _ in range(n)]
    for i in range(n):
        que = collections.deque(e[i])
        for _ in range(2):
            for __ in range(len(que)):
                tmp = que.popleft()
                for ei in e[tmp]:
                    que.append(ei)
        tmp = set(que)
        tmp -= {i}
        e3[i] = list(tmp)

    ans = -1
    cnt = 1
    que = collections.deque(e3[s])
    finflg = True

    while len(que) and finflg and cnt < 10 ** 5+1:

        cnt += 1
        for _ in range(len(que)):
            q = que.popleft()
            for ei in e3[q]:
                que.append(ei)
                if ei == t:
                    finflg = False
                    ans = cnt

    print((ans if ans != -1 else -1))


if __name__ == '__main__':
    main()
# Original solution
from collections import defaultdict
import sys
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))

N, M = inpl()
uv = [ set() for _ in range(N) ]
for _ in range(M):
    u, v = inpl()
    uv[u-1].add(v-1)
S, T = inpl()
S -= 1
T -= 1

cur = set({S})
for i in range(3*N-2):
    if i % 3 == 0 and T in cur:
        print((i//3))
        break
    prev = cur
    cur = set()
    for p in prev:
        cur |= uv[p]
else:
    print((-1))

# Optimized solution
import sys
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))

N, M = inpl()
uv = [ set() for _ in range(3*N) ]
vu = [ set() for _ in range(3*N) ]
for _ in range(M):
    u, v = inpl()
    for k in range(3):
        uv[u-1 + k*N].add(v-1 + ((k+1)%3)*N)
        vu[v-1 + ((k+1)%3)*N].add(u-1 + k*N)
S, T = inpl()
S -= 1
T -= 1

cur = set({S})
for i in range(3*N):
    if i % 3 == 0 and T in cur:
        print((i//3))
        break
    elif len(cur) == 0:
        print((-1))
        break
    prev = cur
    cur = set()
    for u in prev:
        for fr in vu[u]:
            uv[fr].remove(u)
        cur |= uv[u]
else:
    print((-1))
# Original solution
import sys
input=sys.stdin.buffer.readline
inputs=sys.stdin.buffer.readlines
sys.setrecursionlimit(10**9)
n,m=list(map(int,input().split()))
edges=[[] for i in range(3+3*n)]
for i in range(m):
    """#weighted->erase_,__,___=map(int,input().split())
    edges[_].append((__,___))
    #undirected->erase#edges[__].append((_,___))
    """
    _,__=list(map(int,input().split()))
    _*=3;__*=3
    edges[_].append(__+1)
    edges[_+1].append(__+2)
    edges[_+2].append(__)

    #undirected->erase#edges[__].append(_)
    """
"""#weighted->erase

s,T=list(map(int,input().split()))
from collections import deque
dq=deque([])
dq.append((3*s,0))
T*=3
seen=set()
seen.add(3*s)
while dq:
    now,c=dq.popleft()
    if now==T :print((c//3));exit()
    for t in edges[now]:
        if t not in seen:
            dq.append((t,c+1))
            seen.add(now)

print("-1")


# Optimized solution
import sys
input=sys.stdin.buffer.readline
inputs=sys.stdin.buffer.readlines
sys.setrecursionlimit(10**9)
n,m=list(map(int,input().split()))
edges=[[] for i in range(3+3*n)]
o=inputs()
s,T=list(map(int,o.pop().split()))
for i in o:
    _,__=list(map(int,i.split()))
    _*=3;__*=3
    edges[_].append(__+1)
    edges[_+1].append(__+2)
    edges[_+2].append(__)

from collections import deque
dq=deque([])
dq.append((3*s,0))
T*=3
seen=[0]*(3*n+3)
seen[3*s]=1
while dq:
    now,c=dq.popleft()
    if now==T :print((c//3));exit()
    for t in edges[now]:
        if seen[t]==0:
            dq.append((t,c+1))
            seen[t]=1
print("-1")
# Original solution
import sys
input=sys.stdin.buffer.readline
inputs=sys.stdin.buffer.readlines
sys.setrecursionlimit(10**9)
n,m=list(map(int,input().split()))
edges=[[] for i in range(3+3*n)]
o=inputs()
s,T=list(map(int,o.pop().split()))

for i in o:
    """#weighted->erase_,__,___=map(int,input().split())
    edges[_].append((__,___))
    #undirected->erase#edges[__].append((_,___))
    """
    _,__=list(map(int,i.split()))
    _*=3;__*=3
    edges[_].append(__+1)
    edges[_+1].append(__+2)
    edges[_+2].append(__)

    #undirected->erase#edges[__].append(_)
    """
"""#weighted->erase

from collections import deque
dq=deque([])
dq.append((3*s,0))
T*=3
seen=set()
seen.add(3*s)
while dq:
    now,c=dq.popleft()
    if now==T :print((c//3));exit()
    for t in edges[now]:
        if t not in seen:
            dq.append((t,c+1))
            seen.add(now)

print("-1")


# Optimized solution
import sys
input=sys.stdin.buffer.readline
inputs=sys.stdin.buffer.readlines
sys.setrecursionlimit(10**9)
n,m=list(map(int,input().split()))
edges=[[] for i in range(3+3*n)]
o=inputs()
s,T=list(map(int,o.pop().split()))
for i in o:
    _,__=list(map(int,i.split()))
    _*=3;__*=3
    edges[_].append(__+1)
    edges[_+1].append(__+2)
    edges[_+2].append(__)

from collections import deque
dq=deque([])
dq.append((3*s,0))
T*=3
seen=set()
seen.add(3*s)
while dq:
    now,c=dq.popleft()
    if now==T :print((c//3));exit()
    for t in edges[now]:
        if t not  in seen:
            dq.append((t,c+1))
            seen.add(t)
print("-1")
# Original solution
from collections import deque
n,m = list(map(int,input().split()))
l = [[] for i in range(n+1)]
rl = [0 for i in range(n+1)]
for i in range(m):
    u,v = list(map(int,input().split()))
    l[u].append(v)
s,t = list(map(int,input().split()))
d = deque()
d.append([s,0])
rl[s] = 1
ans = -1
def kkp(l,rl,now,d,c,t):
    for i in l[now]:
        for j in l[i]:
            for k in l[j]:
                #print(k,now,c)
                if k == t:
                    return c+1
                else:
                    if rl[k] == 0:
                        d.append([k,c+1])
                        rl[k] = 1
    return 0
while len(d):
    now,c = d.popleft()
    if m == 0:
        break
    else:
        if kkp(l,rl,now,d,c,t) > 0:
            ans =  kkp(l,rl,now,d,c,t)
            break
print(ans)

# Optimized solution
from collections import deque
n,m = list(map(int,input().split()))
l = [[] for i in range(n+1)]
rl = [[-1 for i in range(n+1)] for i in range(3)]
for i in range(m):
    u,v = list(map(int,input().split()))
    l[u].append(v)
s,t = list(map(int,input().split()))
d = deque()
ans = -1
d.append([s,0])
rl[0][s] = 0
while len(d):
    now,count = d.popleft()
    for i in l[now]:
        if i == t and count % 3 == 2:
            ans = (count + 1)//3
            break
        else:
            if rl[(count%3 + 1)%3][i] == -1:
                d.append([i,count+1])
                rl[(count%3+1)%3][i] = count+1
    if ans >= 0:
        break
print(ans)
# Original solution
import sys #追加
sys.setrecursionlimit(500*500) #追加
n,m=list(map(int,input().split()))
edge = [[] for i in range(n*3)]
#edge[i] : iから出る道の[行先]の配列
for i in range(m):
    x,y = list(map(int,input().split()))
    x-=1;y-=1
    x*=3;y*=3
    edge[x  ].append(y+1)
    edge[x+1].append(y+2)
    edge[x+2].append(y  )
    
def dfs(s,g,res):
  if s==g or s==g+1 or s==g+2:
    if res%3==0:
      return res//3
  
  if res>m*3:
    return float("inf")
  
  l=float("inf")
  for i in edge[s]:
    l=min(l,dfs(i,g,res+1))
  return l
s,t=list(map(int,input().split()))
s-=1;t-=1
ans=dfs(s*3,t*3,0)
if ans==float("inf"):
  print((-1))
else:
  print(ans)


# Optimized solution
from collections import deque
n,m=list(map(int,input().split()))
edge = [[] for i in range(n+1)]
#edge[i] : iから出る道の[行先]の配列
for _ in range(m):
    u, v = list(map(int, input().split()))
    edge[u].append(v)
s,t = list(map(int, input().split()))
queue = deque()
queue.append((s, 0))
visited = [[False] * 3 for _ in range(n+1)]
while queue:
    u, d = queue.popleft()
    if visited[u][d % 3]:
        continue
    visited[u][d % 3] = True
    if d % 3 == 0 and u == t:
        print((d // 3));exit()
    for v in edge[u]:
        queue.append((v, d+1))
print((-1))
# Original solution
N,M = list(map(int, input().split()))
g = {}
for v in range(1,N*3+1):
    g[v] = []

for _ in range(M):
    u,v = list(map(int, input().split()))
    g[u + N*0].append(v + N*1)
    g[u + N*1].append(v + N*2)
    g[u + N*2].append(v + N*0)

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

visiting = [(S,0)]
visited = {}
while visiting:
    v,d = visiting.pop(0)
    if v in visited:
        continue
    visited[v] = d
    if v == T:
        break

    for v2 in g[v]:
        if v2 in visiting or v2 in visited:
            continue
        visiting.append((v2,d+1))

if T in visited:
    print((visited[T] // 3))
else:
    print((-1))


# Optimized solution
N,M = list(map(int, input().split()))
g = {}
for v in range(1,N*3+1):
    g[v] = []

for _ in range(M):
    u,v = list(map(int, input().split()))
    g[u + N*0].append(v + N*1)
    g[u + N*1].append(v + N*2)
    g[u + N*2].append(v + N*0)

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

visiting = [S]
visited = {S:0}
while visiting:
    v = visiting.pop(0)
    if v == T:
        break

    for v2 in g[v]:
        if v2 not in visited:
            visiting.append(v2)
            visited[v2] = visited[v]+1

if T in visited:
    print((visited[T] // 3))
else:
    print((-1))
# Original solution
from collections import deque

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

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

memo = {}
def search(node, k):
  global memo
  s = set()
  if k == 0:
    return set([node])
  if (node, k) in memo:
    return memo[(node, k)]
  children = graph[node]
  for child in children:
    s |= search(child, k-1)
  memo[(node, k)] = s
  return s


# 距離が3のノード間にだけ辺を張り直す
g = [[] for i in range(N)]
for i in range(N):
  g[i] = search(i, 3)

dist = [-1] * N
dist[S] = 0
d = deque([[S, -1]])
while d:
  node, par = d.popleft()
  children = g[node]
  for child in children:
    if dist[child] < 0:
      dist[child] = dist[node] + 1
      d.append([child, node])

if dist[T] > 0:
  print((dist[T]))
else:
  print((-1))

# Optimized solution
from collections import deque

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

S, T = list(map(int, input().split()))
S -= 1; T -= 1
dist = [-1] * (3*N)
dist[3*S] = 0
d = deque([3*S])
while d:
  node = d.popleft()
  children = graph[node]
  for child in children:
    if dist[child] < 0:
      dist[child] = dist[node] + 1
      d.append(child)

if dist[3*T] > 0:
  print((dist[3*T] // 3))
else:
  print((-1))
# Original solution
#!/usr/bin/env python3
import sys
import heapq
from collections import defaultdict


def solve(N: int, M: int, U: "List[int]", V: "List[int]", S: int, T: int):
    S -= 1
    T -= 1
    adj = [[] for i in range(N)]
    for u, v in zip(U, V):
        adj[u-1].append(v-1)
    color = [[] for i in range(N)]
    color[S] = [0]
    Q = [(0, S)]

    while len(Q) > 0:
        d, u = heapq.heappop(Q)
        if u == T and d % 3 == 0:
            print((d // 3))
            return
        for v in adj[u]:
            if (d+1) % 3 in color[v]:
                continue
            heapq.heappush(Q, (d+1, v))
            color[v].append(d+1 % 3)

    print((-1))
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    u = [int()] * (M)  # type: "List[int]" 
    v = [int()] * (M)  # type: "List[int]" 
    for i in range(M):
        u[i] = int(next(tokens))
        v[i] = int(next(tokens))
    S = int(next(tokens))  # type: int
    T = int(next(tokens))  # type: int
    solve(N, M, u, v, S, T)

if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys
import heapq
from collections import defaultdict


def solve(N: int, M: int, U: "List[int]", V: "List[int]", S: int, T: int):
    S -= 1
    T -= 1
    adj = [[] for i in range(N)]
    for u, v in zip(U, V):
        adj[u-1].append(v-1)
    color = [[0, 0, 0] for i in range(N)]
    color[S][0] = 1
    Q = [(0, S)]

    while len(Q) > 0:
        d, u = heapq.heappop(Q)
        if u == T and d % 3 == 0:
            print((d // 3))
            return
        for v in adj[u]:
            if color[v][(d+1) % 3] == 1:
                continue
            heapq.heappush(Q, (d+1, v))
            color[v][(d+1) % 3] = 1

    print((-1))
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    u = [int()] * (M)  # type: "List[int]" 
    v = [int()] * (M)  # type: "List[int]" 
    for i in range(M):
        u[i] = int(next(tokens))
        v[i] = int(next(tokens))
    S = int(next(tokens))  # type: int
    T = int(next(tokens))  # type: int
    solve(N, M, u, v, S, T)

if __name__ == '__main__':
    main()
# Original solution
N,M=list(map(int,input().split()))
E=[[] for i in range(N)]
for i in range(M):
  u,v=list(map(int,input().split()))
  u,v=u-1,v-1
  E[u].append(v)
S,T=list(map(int,input().split()))
S,T=S-1,T-1

from collections import deque
stack=deque()
seen=[[False for i in range(3)] for j in range(N)]
stack.append([S,0,1])
while stack:
  node=stack.popleft()
  v=node[0]
  step=node[1]
  turn=node[2]
  if step==3:
    if v==T:
      print(turn)
      break
    step=0
    turn+=1
  if seen[v][step]:
    continue
  seen[v][step]=True
  children=E[v]
  for child in children:
    stack.append([child,step+1,turn])
else:
  print((-1))


# Optimized solution
N,M=list(map(int,input().split()))
E=[[] for i in range(N)]
for i in range(M):
  u,v=list(map(int,input().split()))
  u,v=u-1,v-1
  E[u].append(v)
S,T=list(map(int,input().split()))
S,T=S-1,T-1

from collections import deque
q=deque()
seen=[[False for i in range(3)] for j in range(N)]
q.append([S,0,1])
while q:
  node=q.popleft()
  v=node[0]
  step=node[1]
  turn=node[2]
  if step==3:
    if v==T:
      print(turn)
      break
    step=0
    turn+=1
  if seen[v][step]:
    continue
  seen[v][step]=True
  children=E[v]
  for child in children:
    q.append([child,step+1,turn])
else:
  print((-1))
# Original solution
N, M = [int(x) for x in input().strip().split(" ")]

vs = set()
def dfs_w(v, d, t):
    # dfs while版 
    r = -1
    stack = [v]
    path = []
    while True:
        if len(stack) == 0:
            break
        v = stack.pop()
        if v < 0:
            path.pop()
        else:
            path.append(v)
            vs.add(v)
            for u in d[v]:
                if u == t:
                    #print(r,path)
                    if r != -1:
                        r = min(len(path)//3, r)
                    else:
                        r = len(path)//3
                    #continue
                if u in vs:
                    continue
                stack += [-v, u]
    return r

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

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

s, t = [int(x) for x in input().strip().split(" ")]
print((bfs(s, d, t, 3*N+1)))

# Optimized solution
N, M = [int(x) for x in input().strip().split(" ")]
from collections import deque
vs = set()
def bfs(v,d,t,N):
    queue=deque([v])
    dist = [0] * (N+1)
    while True:
        if len(queue) == 0:
            break
        v = queue.popleft()
        for u in d[v]:
            if u in vs:
                continue
            vs.add(u)
            dist[u] = dist[v] + 1
            if u == t:
                return dist[u]//3
            queue.append(u)
    return -1

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

s, t = [int(x) for x in input().strip().split(" ")]
print((bfs(s, d, t, 3*N+1)))
# Original solution
N, M = [int(x) for x in input().strip().split(" ")]
from collections import deque
vs = set()
def bfs(v,d,t,N):
    queue=deque([v])
    dist = [0] * (N+1)
    while True:
        if len(queue) == 0:
            break
        v = queue.popleft()
        for u in d[v]:
            if u in vs:
                continue
            vs.add(u)
            dist[u] = dist[v] + 1
            if u == t:
                return dist[u]//3
            queue.append(u)
    return -1

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

s, t = [int(x) for x in input().strip().split(" ")]
print((bfs(s, d, t, 3*N+1)))


# Optimized solution
N,M=list(map(int,input().split()))
edge=[list(map(int,input().split())) for i in range(M)]
inf=10**20
c=[[] for i in range(N*3+1)]
for i,j in edge:
    c[i].append(N+j)
    c[N+i].append(N+N+j)
    c[N+N+i].append(j)
S,T=list(map(int, input().split()))
from collections import deque
d=[inf]*(N*3+1)
d[S]=0
q=deque([S])
x=1
while q:
    q2=deque()
    for i in q:
        for j in c[i]:
            if d[j]>x:
                d[j]=x
                q2.append(j)
    q=q2
    x+=1
print((d[T]//3 if d[T]<inf else -1))
# Original solution
import heapq
N,M = list(map(int,input().split()))
edge = [[] for i in range(N)]
double_edge = [[] for i in range(N)]
triple_edge = [[] for i in range(N)]

for i in range(M):
	u,v = list(map(int,input().split()))
	u -= 1
	v -= 1
	edge[u].append(v)
S,T = list(map(int,input().split()))
S -= 1
T -= 1
for i in range(N):
	e = edge[i]
	l = []
	for x in e:
		l += edge[x]
	double_edge[i] = list(set(l))
for i in range(N):
	e = edge[i]
	l = []
	for x in e:
		l += double_edge[x]
	l = list(set(l))
	if i in l:
		l.remove(i)
	triple_edge[i] = l

INF = 10**10
dist = [INF if i != S else 0 for i in range(N)]
search = []
searched = [False for i in range(N)]
heapq.heapify(search)
heapq.heappush(search, (0,S))
while len(search) > 0 and dist[T] == INF:
	x = heapq.heappop(search)
	d, node = x[0], x[1]
	if searched[node]:
		continue
	searched[S] = True
	dist[node] = d
	for i in triple_edge[node]:
		if not searched[i]:
			heapq.heappush(search, (min(d+1, dist[i]), i))
if dist[T] < INF:
	print((dist[T]))
else:
	print((-1))

# Optimized solution
import heapq
N,M = list(map(int,input().split()))
edge = [[[] for i in range(N)] for j in range(3)]

for i in range(M):
	u,v = list(map(int,input().split()))
	u -= 1
	v -= 1
	edge[0][u].append(v)
	edge[1][u].append(v)
	edge[2][u].append(v)
S,T = list(map(int,input().split()))
S -= 1
T -= 1
INF = 10**10
dist = [[0 if i == S and j == 0 else INF for i in range(N)] for j in range(3)]
search = []
searched = [[False for i in range(N)] for j in range(3)]
heapq.heapify(search)
heapq.heappush(search, (0, 0, S))
while len(search) > 0 and dist[0][T] == INF:
	x = heapq.heappop(search)
	d, t, node = x[0], x[1], x[2]
	if searched[t][node]:
		continue
	searched[t][node] = True
	dist[t][node] = d
	for i in edge[t][node]:
		if not searched[(t+1)%3][i]:
			heapq.heappush(search, (d+(t+1)//3, (t+1) % 3, i))
if dist[0][T] < INF:
	print((dist[0][T]))
else:
	print((-1))