CodeEff/ECCO huggingface dataset:
# Original solution
N,M = list(map(int,input().split()))
path = [[] for _ in range(N*3+3)]
for _ in range(M):
    u,v = list(map(int,input().split()))
    path[u].append(v+N)
    path[u+N].append(v+N+N)
    path[u+N+N].append(v)

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

q = collections.deque()
vis = [-1 for _ in range(N*3+3)]
def bfs():
    while len(q) > 0:
        now,cnt = q.popleft()
        #print(now,T,cnt,path[now],q)
        vis[now] = cnt
        if now == T: 
            return cnt
        for x in path[now]:
            if vis[x] == -1:
                q.append((x,cnt+1))
    return -3
vis[S] = 1
q.append([S,0])
print((bfs()//3))




# Optimized solution
N,M = list(map(int,input().split()))
path = [[] for _ in range(N*3+3)]
for _ in range(M):
    u,v = list(map(int,input().split()))
    path[u].append(v+N)
    path[u+N].append(v+N+N)
    path[u+N+N].append(v)

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

q = collections.deque()
vis = [-1 for _ in range(N*3+3)]
def bfs():
    while len(q) > 0:
        now,cnt = q.popleft()
        #print(now,T,cnt,path[now],q)
        vis[now] = cnt
        if now == T: 
            return cnt
        [q.append((x,cnt+1)) for x in path[now] if vis[x] == -1]
        #for x in path[now]:
         #   if vis[x] == -1:
          #      q.append((x,cnt+1))
    return -3
vis[S] = 1
q.append([S,0])
print((bfs()//3))
# Original solution
N,M = list(map(int,input().split()))
import time
start_t = time.time()
path = [[] for _ in range(N+3)]
for _ in range(M):
    u,v = list(map(int,input().split()))
    path[u].append(v)

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

q = collections.deque()
vis = [[-1 for _ in range(3)] for _ in range(N+3)]
vis[S][0] = 1
q.append([S,0])
while len(q):
    now,cnt = q.popleft()
    step = cnt % 3
    vis[now][step] = cnt
    for x in path[now]:
        if vis[x][(step+1)%3] < 0:
            q.append((x,cnt+1))
if vis[T][0] == -1:
    print((-1))
else:
    print((vis[T][0]//3))



# Optimized solution
N,M = list(map(int,input().split()))
import time
start_t = time.time()
path = [[] for _ in range(N+3)]
for _ in range(M):
    u,v = list(map(int,input().split()))
    path[u].append(v)

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

q = collections.deque()
vis = [[-1 for _ in range(3)] for _ in range(N+3)]
vis[S][0] = 1
q.append([S,0])
while len(q):
    now,cnt = q.popleft()
    step = cnt % 3
    for x in path[now]:
        if vis[x][(step+1)%3] < 0:
            vis[x][(step+1)%3] = cnt
            q.append((x,cnt+1))

if vis[T][0] == -1:
    print((-1))
else:
    print((vis[T][0]//3+1))
# Original solution
def main():
  N, M = list(map(int, input().split()))
  edge = [[] for _ in range(N)]
  for i in range(M):
    u, v = list(map(int, input().split()))
    edge[u-1].append(v-1)
  d = [-1]*N
  S, T = list(map(int, input().split()))
  d[S-1] = 0
  que = [S-1]
  while len(que)>0:
    now = que.pop(0)
    for i in edge[now]:
      for j in edge[i]:
        for k in edge[j]:
          if d[k] == -1:
            que.append(k)
            d[k] = d[now]+1 
  print((d[T-1]))
  
if __name__ == "__main__":
  main()

# Optimized solution
def main():
  N, M = list(map(int, input().split()))
  edge = [[[] for _ in range(3)] for _ in range(N)]
  for i in range(M):
    u, v = list(map(int, input().split()))
    edge[u-1][0].append([v-1,1])
    edge[u-1][1].append([v-1,2])
    edge[u-1][2].append([v-1,0])
  d = [[-1]*3 for _ in range(N)]
  S, T = list(map(int, input().split()))
  d[S-1][0] = 0
  que = [[S-1, 0]]
  while len(que)>0:
    now = que.pop(0)
    for nex in edge[now[0]][now[1]]:
      if d[nex[0]][nex[1]] != -1:continue
      d[nex[0]][nex[1]] = d[now[0]][now[1]]
      if now[1] == 2:
        d[nex[0]][nex[1]] += 1
      que.append(nex)
  print((d[T-1][0]))
  
if __name__ == "__main__":
  main()
# Original solution
import sys
from collections import deque

N, M = list(map(int, sys.stdin.readline().split()))

edges = [[] for _ in range(N)]
for _ in range(M):
    u, v = list(map(int, sys.stdin.readline().split()))
    edges[u-1].append(v-1)

S, T = list(map(int, sys.stdin.readline().split()))
S -= 1
T -= 1

visited = {}
q = deque()
q.append((S, 0))
d = -1
while q:
    s, c = q.popleft()
    if s in visited and visited[s] % 3 == c % 3:
        continue
    visited[s] = c
    for n in edges[s]:
        if n == T and c % 3 == 2:
            d = c + 1
            break
        q.append((n, c+1))

    if d % 3 == 0:
        break

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

# Optimized solution
import sys
from collections import deque

N, M = list(map(int, sys.stdin.readline().split()))

edges = [[] for _ in range(N)]
for _ in range(M):
    u, v = list(map(int, sys.stdin.readline().split()))
    edges[u-1].append(v-1)

S, T = list(map(int, sys.stdin.readline().split()))
S -= 1
T -= 1

visited = {}
q = deque()
q.append((S, 0))
d = -1
while q:
    s, c = q.popleft()
    if s in visited and (c % 3) in visited[s]:
        continue
    if s not in visited:
        visited[s] = set()
    visited[s].add(c % 3)
    for n in edges[s]:
        if n == T and c % 3 == 2:
            d = c + 1
            break
        q.append((n, c+1))

    if d % 3 == 0:
        break

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

N,M=list(map(int,input().split()))
# 頂点の番号は0-initialのまま扱う
E=set() # 辺から成るset
for i in range(M):
    u,v=list(map(int,input().split()))
    E.add((u,v))
S,T=list(map(int,input().split()))

d1=[set() for i in range(N+1)] # d1[i]=頂点iからの距離が1であるような頂点vから成るset
for e in E:
    u=e[0];v=e[1]
    d1[u].add(v)

d2=[set() for i in range(N+1)] # d2[i]=頂点iからの距離が2であるような頂点vから成るset
for e in E:
    u=e[0];v=e[1]
    d2[u].update(d1[v])

d3=[set() for i in range(N+1)]
for e in E:
    u=e[0];v=e[1]
    d3[u].update(d2[v])
'''
print(e)
print(d1)
print(d2)
print(d3)
'''
dis=[-1 for i in range(N+1)];dis[S]=0 # 頂点Sからの距離
# WFS
q=deque() # 遅ければlistで実装
q.append(S)
while q:
#    print(q) #DB
#    print(dis) #DB
    u=q.popleft()
    vs=[v for v in d3[u] if dis[v]==-1]
    for v in vs:
        dis[v]=dis[u]+1
    q.extend(vs)

print((dis[T]))

# Optimized solution
from collections import deque

N,M=list(map(int,input().split()))
a=[set() for i in range(N)] # 0-index
for i in range(M):
    u,v=[int(x)-1 for x in input().split()]
    a[u].add(v)
S,T=[int(x)-1 for x in input().split()]
# print(a)
t=[]
for i in range(N):    
    ti=set()
    for u in a[i]:
        for v in a[u]:
            for w in a[v]:
                ti|={w}    
    t.append(ti)
# print(t)
r=[-1 for i in range(N)]
q=deque()
q.append(S);r[S]=0
while q:
    u=q.popleft()
    for v in t[u]:
        if r[v]==-1:
            r[v]=r[u]+1
            q.append(v)
# print(r)
print((r[T]))
# Original solution
from collections import deque
N, M = list(map(int, input().split()))
edges = [list(map(int, input().split())) for _ in range(M)]
S, T = list(map(int, input().split()))

graph = [[] for _ in range(N + 1)]
for u, v in edges:
    graph[u].append(v)

INF = 10 ** 9 + 7
dp = [[INF] * (N + 1) for _ in range(3)]
dp[0][S] = 0
q = deque()
q.append((S, 0))

while q:
    v, t = q.popleft()
    u = (t + 1) % 3
    d = dp[t][v]
    if t == 2:
        d += 1
    for w in graph[v]:
        if dp[u][w] != INF:
            continue
        dp[u][w] = d
        q.append((w, u))
        
print((dp[0][T] if dp[0][T] < INF else -1))



# Optimized solution
from collections import deque

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

graph = [[] for _ in range(N + 1)]
for u, v in X:
    graph[u].append(v)

INF = 10 ** 9 + 7
ctr = [[INF] * (N + 1) for _ in range(3)]
ctr[0][S] = 0
stack = deque()
stack.append((0, S))
while stack:
    i, u = stack.popleft()
    j = (i + 1) % 3
    for v in graph[u]:
        if ctr[j][v] > ctr[i][u] + 1:
            ctr[j][v] = ctr[i][u] + 1
            stack.append((j, v))
            
if ctr[0][T] < INF:
    print((ctr[0][T] // 3))
else:
    print((-1))
# Original solution
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

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 = {}
for i in range(N):
    used[i] = -1

# print(routes)
def skip(now,move_cnt):
    flag = True
    if move_cnt % 3 == 0:
        if used[now] == -1:
            used[now] = move_cnt//3
        elif used[now] > move_cnt//3:
            used[now] = move_cnt//3
        else:
            flag = False
    if flag:
        for route in routes[now]:
            skip(route,move_cnt+1)

skip(S,0)
try:
    ans = used[T]
except:
    ans = -1
print(ans)


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

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 = [-1 for i in range(N)]

# print(routes)
def skip(now,move_cnt):
    flag = True
    if move_cnt % 3 == 0:
        if used[now] == -1:
            used[now] = move_cnt//3
        elif used[now] > move_cnt//3:
            used[now] = move_cnt//3
        else:
            flag = False
    if flag:
        for route in routes[now]:
            skip(route,move_cnt+1)

skip(S,0)
try:
    ans = used[T]
except:
    ans = -1
print(ans)
# Original solution
n, m = list(map(int, input().split()))
uv = [list(map(int, input().split())) for _ in range(m)]
alist = [[] for _ in range(n)]
for u, v in uv:
    alist[u-1].append(v-1)
s, t = list(map(int, input().split()))

alist3 = [set([]) for _ in range(n)]
for v in range(n):
    for v1 in alist[v]:
        for v2 in alist[v1]:
            for v3 in alist[v2]:
                alist3[v].add(v3)

# BFS
queue = [s-1]
visited = [-1]*n
visited[s-1] = 0
while len(queue) > 0:
    q = queue.pop(0)
    for v in alist3[q]:
        if visited[v] < 0:
            queue.append(v)
            visited[v] = visited[q] + 1
print((visited[t-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()))

alist = [[] for _ in range(3*n)]
for u, v in uv:
    u, v = u-1, v-1
    alist[u].append(n+v)
    alist[n+u].append(2*n+v)
    alist[2*n+u].append(v)


# BFS
queue = [s-1]
visited = [-3]*(3*n)
visited[s-1] = 0
while len(queue) > 0:
    q = queue.pop(0)
    for v in alist[q]:
        if visited[v] < 0:
            queue.append(v)
            visited[v] = visited[q] + 1
print((visited[t-1]//3))
# Original solution
import sys
input = sys.stdin.readline
N, M = list(map(int, input().split()))

def f(D, S, T):
    close = [[False] * N, [False] * N, [False] * N]
    Q = []
    index = 0
    Q.append((S, 0, 0))

    while len(Q) > index:
        p, c, l = Q[index]
        index += 1
        if l == 0 and p == T:
            return c
        close[l][p] = True

        l, c = (l + 1) % 3, c + (l + 1) // 3
        for n in D[p]:
            if close[l][n]:
                continue
            Q.append((n, c, l))
    return -1

if __name__ == "__main__":
    D = {n: set() for n in range(N)}
    for m in range(M):
        u, v = list(map(int, input().split()))
        u -= 1
        v -= 1
        D[u].add(v)

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

    print((f(D, S, T)))


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

def f(D, S, T):
    close = [[False] * N, [False] * N, [False] * N]
    Q = []
    index = 0
    Q.append((S, 0, 0))

    while len(Q) > index:
        p, c, l = Q[index]
        index += 1
        
        if close[l][p]:
            continue
        
        if l == 0 and p == T:
            return c // 3
        close[l][p] = True

        l, c = (l + 1) % 3, c + 1
        for n in D[p]:
            if close[l][n]:
                continue
            Q.append((n, c, l))
    return -1

if __name__ == "__main__":
    D = {n: set() for n in range(N)}
    for m in range(M):
        u, v = list(map(int, input().split()))
        u -= 1
        v -= 1
        D[u].add(v)

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

    print((f(D, S, T)))
# Original solution
import queue
def dist(s, t, g):
    q = queue.Queue()
    dists = [-1]*len(g)
    dists[s] = 0
    q.put(s)
    f = False
    while not q.empty():
        target = q.get()
        for v in g[target]:
            if dists[v] == -1:
                dists[v] = dists[target] + 1
                if v == t:
                    f = True
                    break
                q.put(v)
        if f:
            break
    return dists[t]

def main():
    n, m = list(map(int, input().split()))
    g = [[] for _ in range(n*3)]
    for _ in range(m):
        u, v = list(map(int, input().split()))
        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 -= 1
    t -= 1
    print((dist(s, t, g)//3))

if __name__ == "__main__":
    main()

# Optimized solution
def dist(s, t, g):
    q = [s]
    dists = [-1]*len(g)
    dists[s] = 0
    while len(q) != 0:
        p = []
        for u in q:
            for v in g[u]:
                if dists[v] != -1:
                    continue
                p.append(v)
                dists[v] = dists[u] + 1
        q = p
    return dists[t]

def main():
    n, m = list(map(int, input().split()))
    g = [[] for _ in range(n*3)]
    for _ in range(m):
        u, v = list(map(int, input().split()))
        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 -= 1
    t -= 1
    print((dist(s, t, g)//3))

if __name__ == "__main__":
    main()
# Original solution
import heapq
N, M = list(map(int, input().split()))
Edge = [list(map(int, input().split())) for i in range(M)]

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


G3 = [[] for i in range(3 * N)]
for u, v in Edge:
    u, v = u - 1, v - 1
    G3[u].append([v + N, 1])
    G3[u + N].append([v + N * 2, 1])
    G3[u + 2 * N].append([v, 1])


def dijkstra(x):
    d = [float('inf')] * (3 * N)
    d[x] = 0
    visited = {x}

    # d, u
    hq = [(0, x)]

    while hq:
        u = heapq.heappop(hq)[1]
        visited.add(u)

        for node, cost in G3[u]:
            if (node not in visited) and d[node] > d[u] + cost:
                d[node] = d[u] + cost
                heapq.heappush(hq, (d[u] + cost, node))
    return d


dist = dijkstra(S)
print((dist[T] // 3 if dist[T] != float('inf') else - 1))


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

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


def dijkstra(graph, start, inf=float('inf')):
    import heapq
    n = len(graph)
    distances = [inf] * n
    distances[start] = 0
    visited = [False] * n

    # 距離・頂点
    hq = [(0, start)]
    while hq:
        dist, fr = heapq.heappop(hq)
        visited[fr] = True

        for to, cost in graph[fr]:
            new_dist = distances[fr] + cost
            if (visited[to]) or (distances[to] <= new_dist):
                continue

            distances[to] = new_dist
            heapq.heappush(hq, (new_dist, to))

    return distances


distances = dijkstra(G, S)
print((distances[T] // 3 if distances[T] != float('inf') else -1))
# Original solution
from collections import deque

N,M,*f = list(map(int, open(0).read().split()))
uv = [f[i:i+2] for i in range(0, len(f)-2, 2)]
S = f[-2]
T = f[-1]
connected = [[] for _ in range(N+1)]
for u, v in uv:
    connected[u].append(v)

d = deque([S])
for i in range(1,N+1):
    for j in range(3):
        for k in range(len(d)):
            temp = d.popleft()
            for edge in connected[temp]:
                if edge == T:
                    if j == 2:
                        print(i)
                        exit()
                    else:
                        d.append(edge)
                else:
                    d.append(edge)
else:
    print('-1')

# Optimized solution
from collections import deque

N,M,*f = list(map(int, open(0).read().split()))
uv = [f[i:i+2] for i in range(0, len(f)-2, 2)]
S = f[-2]
T = f[-1]
connected = [[] for _ in range(N+1)]
for u, v in uv:
    connected[u].append(v)
dist = [[-1] * 3 for _ in range(N+1)]
dist[S][0] = 0
ans = 0
d = deque([(S,0)])
while d:
    temp = d.popleft()
    for edge in connected[temp[0]]:
        if edge == T and temp[1] == 2:
            print(((dist[temp[0]][2]+1)//3))
            break
        else:
            if dist[edge][(temp[1]+1)%3] < 0:
                dist[edge][(temp[1]+1)%3] = dist[temp[0]][temp[1]] + 1
                d.append((edge,(temp[1]+1)%3))
    else:
        continue
    break
else:
    print((-1))
# Original solution
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, v = u-1, v-1
    graph[u].append(v)
S, T = list(map(int, input().split()))
S, T = S-1, T-1
new1 = [[] for i in range(N)]
new2 = [[] for i in range(N)]
new3 = [[] for i in range(N)]
for i in range(N):
    for v in graph[i]:
        new1[i].append(v)
for i in range(N):
    for v in graph[i]:
        new2[i].extend(new1[v])
for i in range(N):
    for v in graph[i]:
        new3[i].extend(new2[v])
#print(new1)
#print(new2)
#print(new3)



visit = [False for i in range(N)]
length = [0 for i in range(N)]
def bfs(v, T):
    Q = []
    Q.append(v)
    visit[v] = True
    while len(Q) > 0:
        v = Q.pop(0)
        #print(con)
        con = list(set(new3[v]))
        for u in con:
            if not visit[u]:
                visit[u] = True
                length[u] = length[v] + 1
                Q.append(u)
                #print(u, length[u])
            if u == T:
                return()
bfs(S,T)
#print(length)
if visit[T]:
    print((length[T]))
else:
    print((-1))


# Optimized solution
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, v = u-1, v-1
    graph[u].append(v+N)
    graph[u+N].append(v+2*N)
    graph[u+2*N].append(v)
S, T = list(map(int,input().split()))
S, T = S-1, T-1
visit = [False for i in range(3 * N)]
length = [0 for i in range(3 * N)]
def bfs(v):
    Q = []
    Q.append(v)
    visit[v] = True
    while Q:
        v = Q.pop(0)
        for u in graph[v]:
            if not visit[u]:
                visit[u] = True
                length[u] = length[v] + 1
                Q.append(u)
                #print(u, length[u])
bfs(S)
#print(length)
if visit[T]:
    print((int(length[T] //3)))
else:
    print((-1))
# Original solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N,M,*UV,S,T = list(map(int,read().split()))

it = iter(UV)
graph = [[] for _ in range(1+N+N+N)]
for x,y in zip(it,it):
    x1=x+N; x2=x1+N
    y1=y+N; y2=y1+N
    graph[x].append(y1)
    graph[x1].append(y2)
    graph[x2].append(y)   

INF = 10**15
dist = [INF] * (N+N+N+1)
dist[S] = 0
q = [S]
while q:
    qq = []
    for x in q:
        for y in graph[x]:
            if dist[y] != INF:
                continue
            qq.append(y)
            dist[y] = dist[x] + 1
    q = qq

d = dist[T]

answer = -1 if d == INF else d//3
print(answer)


# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N,M,*UV,S,T = list(map(int,read().split()))

it = iter(UV)
graph = [[] for _ in range(1+N+N+N)]
for x,y in zip(it,it):
    x1=x+N; x2=x1+N
    y1=y+N; y2=y1+N
    graph[x].append(y1)
    graph[x1].append(y2)
    graph[x2].append(y)   

INF = 10**15
dist = [INF] * (N+N+N+1)
dist[S] = 0
q = [S]
while q:
    qq = []
    for x in q:
        for y in graph[x]:
            if dist[y] != INF:
                continue
            qq.append(y)
            dist[y] = dist[x] + 1
            if y == T:
                break
    q = qq

d = dist[T]

answer = -1 if d == INF else d//3
print(answer)
# Original solution
import queue
WHITE = 0
GRAY = 1
BLACK = 2
INFTY = 10**16
def dijkstra(s, V):
    color = [WHITE for i in range(N+1)]
    d = [INFTY for i in range(N+1)]
    d[s] = 0
    q = queue.Queue()
    q.put(s)

    while not q.empty():
        u = q.get()
        color[u] = BLACK

        for v in V[u]:
            if color[v] != BLACK and d[v] == INFTY:
                d[v] = d[u] + 1
                q.put(v)

    return d
def rec(s, i, depth):
    if depth == 3:
        kkpgraph[s].append(i)
        return
    if not graph[i]:
        return
    for node in graph[i]:
        rec(s, node, depth+1)
    return

N, M = list(map(int, input().split()))
graph = [[] for i in range(N+1)]
kkpgraph = [[] for i in range(N+1)]
for i in range(M):
    u, v = list(map(int, input().split()))
    graph[u].append(v)
S, T = list(map(int, input().split()))
for i in range(1, N+1):
    rec(i, i, 0)
d = dijkstra(S, kkpgraph)
if d[T] == INFTY:
    print((-1))
else:
    print((d[T]))


# Optimized solution
import queue
WHITE = 0
GRAY = 1
BLACK = 2
INFTY = 10**16
def bfs(s, V):
    color = [[WHITE for i in range(N+1)] for j in range(3)]
    d = [[INFTY for i in range(N+1)] for j in range(3)]
    d[0][s] = 0
    q = queue.Queue()
    q.put((0, s))
    
    while not q.empty():
        dv, u = q.get()
        color[dv%3][u] = BLACK

        for v in V[u]:
            if color[(dv+1)%3][v] != BLACK and d[(dv+1)%3][v] == INFTY:
                d[(dv+1)%3][v] = d[dv%3][u] + 1
                q.put((d[(dv+1)%3][v], v))

    return d

N, M = list(map(int, input().split()))
graph = [[] for i in range(N+1)]
for i in range(M):
    u, v = list(map(int, input().split()))
    graph[u].append(v)
S, T = list(map(int, input().split()))
d = bfs(S, graph)
if d[0][T] == INFTY:
    print((-1))
else:
    print((d[0][T]//3))
# Original solution
n,m = list(map(int,input().split()))
uv = [[] for _ in range(n)]
for i in range(m):
  u,v = [int(x)-1 for x in input().split()]
  uv[u].append(v)
s,t = [int(x)-1 for x in input().split()]
time = 0
now = {s}
flag = 0
for _ in range(n):
  time += 1
  tmp = set()
  for x in now:
    for y in uv[x]:
      tmp.add(y)
  now = tmp
  time += 1
  tmp = set()
  for x in now:
    for y in uv[x]:
      tmp.add(y)
  now = tmp
  time += 1
  tmp = set()
  for x in now:
    for y in uv[x]:
      tmp.add(y)
  now = tmp
  if t in now:
    flag = 1
    break
if flag == 1:
  print((time//3))
else:
  print((-1))

# Optimized solution
n,m = list(map(int,input().split()))
pst = [[] for _ in range(n)]
for _ in range(m):
  a,b = list(map(int,input().split()))
  a -= 1
  b -= 1
  pst[a].append(b)
s,t = list(map(int,input().split()))
s -= 1
t -= 1
cnt = [[0,0,0] for _ in range(n)]
cnt[s][0] = 1
now = [s]
time = 0
while now:
  time += 1
  last = now
  now = []
  for x in last:
    for y in pst[x]:
      if cnt[y][time%3] == 0:
        cnt[y][time%3] = 1
        now.append(y)
  #print(time,cnt,now)
  if cnt[t][0] == 1:
    print((time//3))
    break
else:
  print((-1))