Ken loves playing "ken-ken-pa" (Japanese hopscotch) on a directed graph G. The graph G has N vertices numbered from 1 to N and M edges. Ken starts at vertex S and aims to reach vertex T by repeating ken-ken-pa. In one ken-ken-pa, he follows an edge from his current vertex three times. The task is to determine if Ken can reach vertex T by repeating ken-ken-pa and find the minimum number of ken-ken-pa needed if possible. Constraints include N (2 ≤ N ≤ 10^5), M (0 ≤ M ≤ min(10^5, N(N-1))), and edge conditions. The input format is N, M, followed by edge pairs and S, T. The output should be the minimum number of ken-ken-pa needed or -1 if Ken cannot reach vertex T from vertex S. Sample inputs and outputs are provided for illustration.

# Buggy version:
from collections import deque
n, m = map(int, input().split())

graph = [[] for _ in range(n+1)]
for _ in range(m):
    u, v = map(int, input().split())
    graph[u].append(v)

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

inf = 10**10
dist = [[-3] * 3 for _ in range(n+1)]
dist[s][0] = 0

q = deque([(s, 0)])

while q:
    now, c = q.pop()
    ne = (c+1) % 3
    for node in graph[now]:
        if dist[node][ne] != -1:
            continue
        dist[node][ne] = dist[now][c] + 1
        q.append((node, ne))

print(dist[t][0] // 3)


# Fixed version:
from collections import deque
n, m = map(int, input().split())

graph = [[] for _ in range(n+1)]
for _ in range(m):
    u, v = map(int, input().split())
    graph[u].append(v)

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

dist = [[-3] * 3 for _ in range(n+1)]
dist[s][0] = 0

q = deque([(s, 0)])

while q:
    now, c = q.popleft()
    ne = (c+1) % 3
    for node in graph[now]:
        if dist[node][ne] != -3:
            continue
        dist[node][ne] = dist[now][c] + 1
        q.append((node, ne))

print(dist[t][0] // 3)

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

import heapq
from collections import defaultdict, deque
g1 = defaultdict(set)
for u, v in uv:
    g1[u-1].add(v-1)

s -= 1
t -= 1

dist = [[float('inf')] * 3 for _ in range(n)]
dist[s][0] = 0

q = [(0, s, 0)] # (dist, pos, kenken)
heapq.heapify(q)

while q:
    du, u, c = heapq.heappop(q)
    if dist[u][c] < du:
        continue
    c2 = (c + 1) % 3
    for v in g1[u]:
        if du + 1 < dist[v][c2]:
            dist[v][c2] = du + 1
            heapq.heappush(q, (dist[v][c2], v, c2))

if dist[t][0] != float('inf'):
    print(d)
else:
    print(-1)


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

import heapq
from collections import defaultdict
g1 = defaultdict(set)
for u, v in uv:
    g1[u-1].add(v-1)

s -= 1
t -= 1

dist = [[float('inf')] * 3 for _ in range(n)]
dist[s][0] = 0

q = [(0, s, 0)] # (dist, pos, kenken)
heapq.heapify(q)

while q:
    du, u, c = heapq.heappop(q)
    if dist[u][c] < du:
        continue
    c2 = (c + 1) % 3
    for v in g1[u]:
        if du + 1 < dist[v][c2]:
            dist[v][c2] = du + 1
            heapq.heappush(q, (dist[v][c2], v, c2))

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

# Buggy version:
import collections
N,M=map(int,input().split())
tree=[[] for _ in range(N)]
for _ in range(M):
    u,v=map(int,input().split())
    tree[u-1].append(v-1)
S,T=map(int,input().split())
s=S-1
t=T-1
ischecked=[[0]*3 for _ in range(N)]
ischecked[s][0]=1
ans=-1
q=collections.deque()
q.append([s,0])
while q:
    v,c=q.pop(0)
    if v==t and c%3==0:
        ans=c//3
        break
    for i in tree[v]:
        if ischecked[i][(c+1)%3]==0:
            q.append([i,c+1])
            ischecked[i][(c+1)%3]==1
print(ans)

# Fixed version:
import collections
N,M=map(int,input().split())
tree=[[] for _ in range(N)]
for _ in range(M):
    u,v=map(int,input().split())
    tree[u-1].append(v-1)
S,T=map(int,input().split())
s=S-1
t=T-1
ischecked=[[0]*3 for _ in range(N)]
ischecked[s][0]=1
ans=-1
q=collections.deque()
q.append([s,0])
while q:
    v,c=q.popleft()
    if v==t and c%3==0:
        ans=c//3
        break
    for i in tree[v]:
        if ischecked[i][(c+1)%3]==0:
            q.append([i,c+1])
            ischecked[i][(c+1)%3]=1
print(ans)

# Buggy version:
from collections import deque

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

s, t = map(int, input().split())
dist = [-1] * (3 * n)
queue = deque([3 * s - 3])
dist[3 * s - 3] = 0
while queue:
  vertex = queue.leftpop()
  for next_vertex in adj_list[vertex]:
    if dist[next_vertex] == -1:
      continue
    dist[next_vertex] = dist[vertex] + 1
    queue.append(next_vertex)
t_distance = dist[3 * t - 3]
if t_distance == -1:
  print(-1)
else:
  print(t_distance // 3)

# Fixed version:
from collections import deque

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

s, t = map(int, input().split())
dist = [-1] * (3 * n)
queue = deque([3 * s - 3])
dist[3 * s - 3] = 0
while queue:
  vertex = queue.popleft()
  for next_vertex in adj_list[vertex]:
    if dist[next_vertex] != -1:
      continue
    dist[next_vertex] = dist[vertex] + 1
    queue.append(next_vertex)
t_distance = dist[3 * t - 3]
if t_distance == -1:
  print(-1)
else:
  print(t_distance // 3)

# Buggy version:
from collections import deque
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(m)]
graph=[[] for i in range(n+1)]
for a,b in ab:
  graph[a].append(b)
  graph[b].append(a)
s,t=map(int,input().split())
visited=[[-1,-1,-1] for i in range(n+1)]
que=deque([(s,0,0)])
while que:
  x,mod,cnt=que.popleft()
  visited[x][mod]=cnt
  if x==t and mod==0:
    break
  for y in graph[x]:
    if visited[y][(mod+1)%3]==-1:
      que.append((y,(mod+1)%3,cnt+1))
print(visited[t][0])

# Fixed version:
import sys
from collections import deque
input = sys.stdin.readline
n,m = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(m)]
graph = [[] for i in range(n+1)]
for a,b in ab:
  graph[a].append(b)
s,t = map(int,input().split())
visited = [[-1,-1,-1] for i in range(n+1)]
que = deque([(s,0,0)])
while que:
  x,mod,cnt = que.popleft()
  visited[x][mod] = cnt
  if x == t and mod == 0:
    break
  for y in graph[x]:
    if visited[y][(mod+1)%3] == -1:
      que.append((y,(mod+1)%3,cnt+1))
      visited[y][(mod+1)%3] = cnt+1
print(visited[t][0]//3)

# Buggy version:
from collections import deque
n,m=map(int,input().split())
g=[[] for _ in range(n)]
for _ in range(m):
    u,v=list(map(int,input().split()))
    g[u-1].append(v-1)
s,t=map(int,input().split())
s-=1;t-=1
INF=10**9
U=[[INF]*n for i in range(3)]
U[0][s]=0
que=deque([(s,0)])
while que:
    v,t=que.popleft()
    u=(t+1)%3
    d=U[u][v]
    if t==2:
        d+=1
    for w in g[v]:
        if U[u][w]!=INF:
            continue
        que.append((w,u))
        U[u][w]=d
print(U[0][t] if U[0][t]<INF else -1)


# Fixed version:
from collections import deque
n,m=map(int,input().split())
g=[[] for _ in range(n)]
for _ in range(m):
    u,v=list(map(int,input().split()))
    g[u-1].append(v-1)
S,T=map(int,input().split())
S-=1;T-=1
INF=10**9
U=[[INF]*n for i in range(3)]
U[0][S]=0
que=deque([(0,S)])
while que:
    t,v=que.popleft()
    u=(t+1)%3
    if t==2:
        U[t][v]+=1
    for w in g[v]:
        if U[u][w]!=INF:
            continue
        que.append((u,w))
        U[u][w]=U[t][v]
print(U[0][T] if U[0][T]<INF else -1)

# Buggy version:
from collections import defaultdict, deque
N, M = map(int, input().split())
G = defaultdict(list)
for _ in range(M):
    u, v = map(int, input().split())
    G[(u-1, 0)].append((v-1, 1))
    G[(u-1, 1)].append((v-1, 2))
    G[(u-1, 2)].append((v-1, 0))
S, T = map(int, input().split())
G3 = defaultdict(list)
dist = defaultdict(lambda : -1)
que = deque([(S-1, 0)])
dist[(S-1, 0)] = 0
while que:
    if v == (T-1, 0):
        break
    v = que.pop()
    d = dist[v]
    for nv in G[v]:
        if dist[nv]>=0:
            continue
        dist[nv] = d+1
        que.append(nv)
print(dist[(T-1, 0)]//3)


# Fixed version:
from collections import defaultdict, deque
N, M = map(int, input().split())
G = defaultdict(list)
for _ in range(M):
    u, v = map(int, input().split())
    G[(u-1, 0)].append((v-1, 1))
    G[(u-1, 1)].append((v-1, 2))
    G[(u-1, 2)].append((v-1, 0))
S, T = map(int, input().split())
dist = defaultdict(lambda : -1)
que = deque([(S-1, 0)])
dist[(S-1, 0)] = 0
while que:
    v = que.popleft()
    d = dist[v]
    for nv in G[v]:
        if dist[nv]>=0:
            continue
        dist[nv] = d+1
        que.append(nv)
print(dist[(T-1, 0)]//3)

# Buggy version:
n, m = map(int, input().split())

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

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

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

from heapq import heappush, heappop


def dijkstra(graph: list, n: int, start: int, INF: int = float('inf')) -> list:
    # graph[node] = [(cost, to)]
    dist = [INF] * n
    num = [0] * n

    dist[start] = 0
    heap = [(0, start)]
    while heap:
        _, v_from = heappop(heap)
        for v_to in graph[v_from]:
            dist_cand = dist[v_from] + 1
            if dist_cand < dist[v_to]:
                dist[v_to] = dist_cand
                heappush(heap, (dist[v_to], v_to))
    return dist
    # dist = [costs to nodes]


inf = float('inf')
dist = dijkstra(graph1, n * 3, s, INF=10**8)
if dist[t] == inf:
    print(-1)
else:
    print(dist[t] // 3)


# Fixed version:
n, m = map(int, input().split())

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

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

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

from heapq import heappush, heappop


def dijkstra(graph: list, n: int, start: int, INF: int = float('inf')) -> list:
    # graph[node] = [(cost, to)]
    dist = [INF] * n
    num = [0] * n

    dist[start] = 0
    heap = [(0, start)]
    while heap:
        _, v_from = heappop(heap)
        for v_to in graph[v_from]:
            dist_cand = dist[v_from] + 1
            if dist_cand < dist[v_to]:
                dist[v_to] = dist_cand
                heappush(heap, (dist[v_to], v_to))
    return dist
    # dist = [costs to nodes]


inf = float('inf')
dist = dijkstra(graph1, n * 3, s)
if dist[t] == inf:
    print(-1)
else:
    print(dist[t] // 3)

# Buggy version:
from itertools import count
N,M = map(int,input().split())

adj = [list() for _ in range(N)]


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

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

visited = [False]*(N*3)
visited[S] = True
q = [S]

for step in count():
    offset = N*(step % 3)
    nq = []
    for u in q:
        for v in adj[u]:
            if not visited[v+offset]:
                nq.append(v)
                visited[v+offset] = True
    q = nq

    if visited[T] or not q:
        break
if visited[T]:
    print(step//3)
else:
    print(-1)

# Fixed version:
from itertools import count
N,M = map(int,input().split())

adj = [list() for _ in range(N)]


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

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

visited = [False]*(N*3)
visited[S] = True
q = [S]

for step in count(1):
    offset = N*(step % 3)
    nq = []
    for u in q:
        for v in adj[u]:
            if not visited[v+offset]:
                nq.append(v)
                visited[v+offset] = True
    q = nq

    if visited[T] or not q:
        break
if visited[T]:
    print(step//3)
else:
    print(-1)

# Buggy version:
from collections import deque
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
G = [[] for i in range(3 * N)]
for i in range(M):
    u, v = map(int, input().split())
    u, v = u - 1, v - 1
    G[3 * u + 0].append(3 * v + 1)
    G[3 * u + 1].append(3 * v + 2)
    G[3 * u + 2].append(3 * v + 0)
S, T = map(int, input().split())
S, T = S - 1, T - 1
INF = float('inf')
d = [INF] * (3 * N)
d[3 * S] = 0


que = deque([(3 * S, 0)])
while que:
    u, k = que.pop()
    d[u] = k
    for w in G[u]:
        if d[w] == INF:
            que.append((w, k + 1))


ans = d[3 * T] // 3
if ans == INF:
    print(-1)
else:
    print(ans)

# Fixed version:
from collections import deque
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
G = [[] for i in range(3 * N)]
for i in range(M):
    u, v = map(int, input().split())
    u, v = u - 1, v - 1
    G[3 * u + 0].append(3 * v + 1)
    G[3 * u + 1].append(3 * v + 2)
    G[3 * u + 2].append(3 * v + 0)
S, T = map(int, input().split())
S, T = S - 1, T - 1
INF = float('inf')
d = [INF] * (3 * N)
d[3 * S] = 0


que = deque([3 * S])
while que:
    u = que.popleft()
    for w in G[u]:
        if d[w] == INF:
            d[w] = d[u] + 1
            que.append(w)


ans = d[3 * T]
if ans == INF:
    print(-1)
else:
    print(ans // 3)

# Buggy version:
N, M = map(int, input().split())
G = [[] for i in range(M+1)]
for i in range(M):
    u, v = map(int, input().split())
    G[u].append(v)
s, t = map(int, input().split())

d = [-1] * (N + 1)

S = set()
S.add(s)
d[s] = 0
x = 1
while len(S) > 0:
    Q1 = set()
    for u in S:
        for v in G[u]:
            Q1.add(v)
    Q2 = set()
    for u in Q1:
        for v in G[u]:
            Q2.add(v)
    Q3 = set()
    for u in Q2:
        for v in G[u]:
            if d[v] < 0:
                d[v] = x
                Q3.add(v)
    x += 1
    S = Q3

print(d[t])


# Fixed version:
N, M = map(int, input().split())
G = [[] for i in range(N+1)]
for i in range(M):
    u, v = map(int, input().split())
    G[u].append(v)
s, t = map(int, input().split())

d = [-1] * (N + 1)

S = set()
S.add(s)
d[s] = 0
x = 1
while len(S) > 0:
    Q1 = set()
    for u in S:
        for v in G[u]:
            Q1.add(v)
    Q2 = set()
    for u in Q1:
        for v in G[u]:
            Q2.add(v)
    Q3 = set()
    for u in Q2:
        for v in G[u]:
            if d[v] < 0:
                d[v] = x
                Q3.add(v)
    x += 1
    S = Q3

print(d[t])

# Buggy version:
from collections import deque

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

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

def bfs():
    q = deque()
    q.append((S, 0))
    dist[S][0] = 0
    while q:
        p = q.popleft()
        v = p[0]
        l = p[1]
        for nv in edge[v]:
            nl = (l+1)%3
            if dist[nv][nl] != INF:
                continue
            dist[nv][nl] = dist[v][l] + 1
            q.append((nv, nl))
    return dist[T][0] // 3

print(bfs())

# Fixed version:
from collections import deque

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

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

def bfs():
    q = deque()
    q.append((S, 0))
    dist[S][0] = 0
    while q:
        p = q.popleft()
        v = p[0]
        l = p[1]
        for nv in edge[v]:
            nl = (l+1)%3
            if dist[nv][nl] != INF:
                continue
            dist[nv][nl] = dist[v][l] + 1
            q.append((nv, nl))
    return dist[T][0]

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

# Buggy version:
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()
    while stack:
        i, d = stack.pop(0)
        if i in visited:
            continue
        visited.add(i)
        a = []
        for j in e[i]:
            a += e[j]
        b = []
        for j in a:
            b += e[j]
        b = set(b)
        if t in b:
            return d + 1
        else:
            stack += [(x, d + 1) for x in list(b)]
        return -1


print(BC132_E())

# Fixed version:
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())

# Buggy version:
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
from collections import defaultdict
branch=defaultdict(set)
for i in range(m):
    u,v=map(int,input().split())
    branch[u-1]|={v-1}
s,t=map(int,input().split())
s-=1
t-=1
dist=[[float('inf')]*3 for i in range(n)]
check={(s,0)}
dist[s][0]=0
while len(check)>0:
    now,nowd=check.pop()
    for nex in branch[now]:
        if dist[nex][(nowd+1)%3]>dist[now][nowd]+1:
            ans[nex][(nowd+1)%3]=ans[now][nowd]+1
            check|={(nex,(nowd+1)%3)}
if dist[t][0]>10**18:
    print(-1)
else:
    print(dist[t][0]//3)

# Fixed version:
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
from collections import defaultdict
branch=defaultdict(set)
for i in range(m):
    u,v=map(int,input().split())
    branch[u-1]|={v-1}
s,t=map(int,input().split())
s-=1
t-=1
dist=[[float('inf')]*3 for i in range(n)]
check={(s,0)}
dist[s][0]=0
while len(check)>0:
    now,nowd=check.pop()
    for nex in branch[now]:
        if dist[nex][(nowd+1)%3]>dist[now][nowd]+1:
            dist[nex][(nowd+1)%3]=dist[now][nowd]+1
            check|={(nex,(nowd+1)%3)}
if dist[t][0]>10**18:
    print(-1)
else:
    print(dist[t][0]//3)

# Buggy version:
# -*- coding: utf-8 -*-
"""
Created on Mon Jul  1 11:43:19 2019

@author: Yamazaki Kenichi
"""
from collections import deque

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

Ta = [[] for i in range(3*N+1)]
for c in uv:
    Ta[c[0]].append(c[1]+N)
    Ta[c[0]+N].append(c[1]+2*N)
    Ta[c[0]+2*N].append(c[1])
del c

check = [0 for i in range(3*N+1)]

q = deque([S])
check[S] = 1
counta = 0
while len(q) > 0:
    for i in range(len(q)):
        nexts = Ta[q.popleft()]
        for next_ in nexts:
            if check[next_] != 1:
                q.append(next_)
                check[next_] = 1
    counta += 1
    if check[T] == 1:
        break

print(counta//3)

# Fixed version:
# -*- coding: utf-8 -*-
"""
Created on Mon Jul  1 11:43:19 2019

@author: Yamazaki Kenichi
"""
from collections import deque

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

Ta = [[] for i in range(3*N+1)]
for c in uv:
    Ta[c[0]].append(c[1]+N)
    Ta[c[0]+N].append(c[1]+2*N)
    Ta[c[0]+2*N].append(c[1])

check = [0 for i in range(3*N+1)]

q = deque([S])
check[S] = 1
counta = 0
flg = False
while len(q) > 0:
    for i in range(len(q)):
        nexts = Ta[q.popleft()]
        for next_ in nexts:
            if check[next_] != 1:
                q.append(next_)
                check[next_] = 1
    counta += 1
    if check[T] == 1:
        flg = True
        break
if flg:
    print(counta//3)
else:
    print(-1)

# Buggy version:
from collections import deque

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

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

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

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

def bfs(s):
    q = deque()
    q.append(s)
    d = [-1 for _ in range(3*N)]
    d[s]
    
    while q:
        v = q.popleft()
        if v == T:
            return d[v]
    
        for u in connect[v]:
            if d[u] == -1:
                q.append(u)
                d[u] = d[v]+1
                
    return -1

ans =  bfs(S)

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

# Fixed version:
from collections import deque

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

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

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

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

def bfs(s):
    q = deque()
    q.append(s)
    d = [-1 for _ in range(3*N)]
    d[s] = 0
    
    while q:
        v = q.popleft()
        if v == T:
            return d[v]
    
        for u in connect[v]:
            if d[u] == -1:
                q.append(u)
                d[u] = d[v]+1
                
    return -1

ans =  bfs(S)

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

# Buggy version:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import numpy as np
import queue

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

def LI(): return list(map(int, sys.stdin.readline().split()))
N,M = LI()

um = [[] for i in range(M)]
for i in range(M):
    um[i] = LI()

S,T = LI()
S -= 1
T -= 1
routes = []
for i in range(N):
    routes[i] = []
for i in range(M):
    u,m = um[i]
    u -= 1
    m -= 1
    routes[u].append(m)

used = np.array([[mod,mod,mod] for i in range(N)])

q = queue.Queue()
q.put((S,0))
# Deep First Serch
while q.empty() == False:
    now, cost = q.get()
    state = cost % 3
    if used[now,state] == mod:
        used[now,state] = cost
        for route in routes[now]:
            q.put((route,cost+1))

# print(used)
try:
    ans = used[T, 0]
    if ans == mod:
        ans = -1
    else:
        ans = ans/3
except:
    ans = -1
print(int(ans))


# Fixed version:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import numpy as np
import queue

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

def LI(): return list(map(int, sys.stdin.readline().split()))
N,M = LI()

um = [[] for i in range(M)]
for i in range(M):
    um[i] = LI()

S,T = LI()
S -= 1
T -= 1
routes = [[] for i in range(N)]

for i in range(M):
    u,m = um[i]
    u -= 1
    m -= 1
    routes[u].append(m)

used = [[mod,mod,mod] for i in range(N)]

from collections import deque

# q = queue.Queue()
q = deque()
q.append((S,0))
# Deep First Serch
while q:
    now, cost = q.popleft()
    state = cost % 3
    if used[now][state] == mod:
        used[now][state] = cost
        for route in routes[now]:
            q.append((route,cost+1))

# print(used)
try:
    ans = used[T][0]
    if ans == mod:
        ans = -1
    else:
        ans = ans/3
except:
    ans = -1
print(int(ans))

# Buggy version:
S, T = [int(c)-1 for c in input().split()]

reach = [[-1 for _ in range(N)] for _ in range(3)]
reach[0][S] = 0

queue={S}

time = 1
for _ in range(4*N):
    queue_new = set()
    for q in queue:
        for v in path[q]:
            if reach[time%3][v]==-1:
                reach[time%3][v]=time
        queue_new.add(v)
		
    queue = queue_new
    time += 1

if reach[0][T]==-1:
    print(-1)
else:
    print(reach[0][T]//3)

# Fixed version:
N, M = [int(c) for c in input().split()]
path = [[] for _ in range(N)]
for i in range(M):
    u, v = [int(c) for c in input().split()]
    path[u-1].append(v-1)

S, T = [int(c)-1 for c in input().split()]

reach = [[-1 for _ in range(N)] for _ in range(3)]
reach[0][S] = 0

queue={S}

time = 1
for _ in range(4*N):
    queue_new = set()
    for q in queue:
        for v in path[q]:
            if reach[time%3][v]==-1:
                reach[time%3][v]=time
                queue_new.add(v)
		
    queue = queue_new
    time += 1

if reach[0][T]==-1:
    print(-1)
else:
    print(reach[0][T]//3)

# Buggy version:
N,M = map(int,input().split())

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

from heapq import heappush, heappop
def dijkstra(graph:list, node:int, start:int) -> list:
    inf = float('inf')
    dist = [inf]*node

    dist[start] = 0
    heap = [(0,start)]
    while heap:
        cost,thisNode = heappop(heap)
        for NextCost,NextNode in graph[thisNode]:
            dist_cand = dist[thisNode]+NextCost
            if dist_cand < dist[NextNode]:
                dist[NextNode] = dist_cand
                heappush(heap,(dist[NextNode],NextNode))
    return dist
S,T = map(int,input().split())
dij = dijkstra(g,3*N,S-1)
ans = dij[T-1]//3
if ans == float('inf') or M==0:
    ans = -1

print(ans)

# Fixed version:
N,M = map(int,input().split())

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

from heapq import heappush, heappop
def dijkstra(graph:list, node:int, start:int) -> list:
    inf = float('inf')
    dist = [inf]*node

    dist[start] = 0
    heap = [(0,start)]
    while heap:
        cost,thisNode = heappop(heap)
        for NextCost,NextNode in graph[thisNode]:
            dist_cand = dist[thisNode]+NextCost
            if dist_cand < dist[NextNode]:
                dist[NextNode] = dist_cand
                heappush(heap,(dist[NextNode],NextNode))
    return dist
S,T = map(int,input().split())
dij = dijkstra(g,3*N,S-1)
ans = dij[T-1]//3
if ans == float('inf') or ans!=ans:
    ans = -1
print(ans)

# Buggy version:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, m = na()
g = []
for i in range(n):
    g.append([])
for i in range(m):
    u, v = na()
    g[u-1].append(v-1)

s, t = na()
s -= 1;
t -= 1

ds = [[9999999999] * n for i in range(3)]
ds[0][s] = 0

q = Queue()
q.put((0, s))
while not q.empty():
    r, cur = q.get_nowait()
    nr = (r+1) % 3
    for e in g[cur]:
        # print(nr, e)
        if ds[nr][e] > ds[r][cur] + 1:
            ds[nr][e] = ds[r][cur] + 1
            q.put((nr, e))
if ds[0][t] >= 0:
    print(ds[0][t]//3)
else:
    print(-1)


# Fixed version:
import sys
from queue import Queue

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, m = na()
g = []
for i in range(n):
    g.append([])
for i in range(m):
    u, v = na()
    g[u-1].append(v-1)

s, t = na()
s -= 1;
t -= 1

ds = [[9999999999] * n for i in range(3)]
ds[0][s] = 0

q = Queue()
q.put((0, s))
while not q.empty():
    r, cur = q.get_nowait()
    nr = (r+1) % 3
    for e in g[cur]:
        # print(nr, e)
        if ds[nr][e] > ds[r][cur] + 1:
            ds[nr][e] = ds[r][cur] + 1
            q.put((nr, e))
if ds[0][t] < 99999999:
    print(ds[0][t]//3)
else:
    print(-1)