CodeEff/ECCO huggingface dataset:
# Original solution
n, m = list(map(int, input().split()))

graph1 = [[] for _ in range(n)]
graph2 = [[] for _ in range(n)]

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


# def dfs(v0, v, k):
#     if k == 0:
#         graph2[v0].append((1, v))
#         return
#
#     for i in range(len(graph1[v])):
#         dfs(v0, graph1[v][i][1], k-1)
#
# for i in range(n):
#     dfs(i, i, 3)
#     # print(graph1[i])

for i in range(n):
    q = [(i, 3)]
    idx = 0
    while True:
        if idx == len(q):
            break
        v, c = q[idx]
        idx += 1
        if c == 0:
            graph2[i].append(v)
            continue
        for j in graph1[v]:
            q.append((j, c-1))



# for i in range(n):
#     print(i)
#     for _, v in graph1[i]:
#         print(" ->", v)
#
#
for i in range(n):
    graph2[i] = list(set(graph2[i]))
# print()
# for i in range(n):
#     print(graph2[i])


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


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

    dist[start] = 0
    heap = [(0,start)]
    NextCost = 1
    while heap:
        cost,thisNode = heappop(heap)
        for 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
    # dist = [costs to nodes]

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



# Optimized solution
n, m = list(map(int, input().split()))

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

for _ in range(m):
    u, v = list(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 = list(map(int, input().split()))
s -= 1
t -= 1
s = s*3
t = t*3

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

    dist[start] = 0
    heap = [(0,start)]
    NextCost = 1
    while heap:
        cost,thisNode = heappop(heap)
        for 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
    # dist = [costs to nodes]

inf = float('inf')
dist = dijkstra(graph1, n*3, s)
if dist[t] == inf:
    print((-1))
else:
    print((dist[t]//3))
# Original solution
n, m = list(map(int, input().split()))

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

for _ in range(m):
    u, v = list(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 = list(map(int, input().split()))
s -= 1
t -= 1
s = s*3
t = t*3

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

    dist[start] = 0
    heap = [(0,start)]
    NextCost = 1
    while heap:
        cost,thisNode = heappop(heap)
        for 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
    # dist = [costs to nodes]

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


# Optimized solution
n, m = list(map(int, input().split()))

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

for _ in range(m):
    u, v = list(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 = list(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))
# Original solution
import sys
from collections import deque
n,m = list(map(int,sys.stdin.readline().split()))
vertices = [[] for _ in range(n+1)]
jumpValue = [[-1,-1,-1] for _ in range(n+1)]
visited = set()
queue = []
temp = []
def bfs(arr,jump):
    temp = []
    for node in arr:
        if jumpValue[node][(jump+1)%3]==-1:
            queue.append([node,jump+1])
        temp = temp+vertices[node]
    return temp
for i in range(m):
    a,b = list(map(int,sys.stdin.readline().split()))
    vertices[a].append(b)
s,t = list(map(int,sys.stdin.readline().split()))
visited.add(s)
queue=deque()
queue.append([s,0])
while(len(queue)>0 ):
    next=queue.popleft()
    if jumpValue[next[0]][next[1]%3] == -1:
        jumpValue[next[0]][next[1]%3] = next[1]
        bfs(vertices[next[0]], next[1])
print((max(jumpValue[t][0]//3,-1)))




# Optimized solution
import sys
from collections import deque
n,m = list(map(int,sys.stdin.readline().split()))
vertices = [[] for _ in range(n+1)]
jumpValue = [[-1,-1,-1] for _ in range(n+1)]
queue = []
def bfs(arr,jump):
    for node in arr:
        if jumpValue[node][(jump+1)%3]==-1:
            queue.append([node,jump+1])
for i in range(m):
    a,b = list(map(int,sys.stdin.readline().split()))
    vertices[a].append(b)
s,t = list(map(int,sys.stdin.readline().split()))
queue=deque()
queue.append([s,0])
while(len(queue)>0 ):
    next=queue.popleft()
    if jumpValue[next[0]][next[1]%3] == -1:
        jumpValue[next[0]][next[1]%3] = next[1]
        bfs(vertices[next[0]], next[1])
        if jumpValue[t][0]!=-1:
            break
print((max(jumpValue[t][0]//3,-1)))
# Original solution
n, m = list(map(int, input().split()))

path_list = [[] for _ in range(n)]

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

s, t = list(map(int, input().split()))
s -= 1
t -= 1
i = 0
dp = [[] for _ in range(3*n)]
dp[0].append(s)
while i < 3*n-1:
  a = set()
  for dot in dp[i]:
    a |= set(path_list[dot])
  if not -~i%3 and t in a:
    print((-~i//3))
    exit()
  dp[i+1] = a
  i += 1
print((-1))

# Optimized solution
n, m = list(map(int, input().split()))

path_list = [[] for _ in range(n)]

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

s, t = list(map(int, input().split()))
s -= 1
t -= 1
i = 0
dp = [set() for _ in range(3)]
dp[0].add(s)
while i < 3*n-1:
  a = set()
  for dot in dp[i%3]:
    a |= set(path_list[dot])
  dp[(i+1)%3] |= a
  if t in dp[0]:
    print((-~i//3))
    exit()
  
  i += 1
print((-1))
# Original solution
import queue 
MOD = 10**9 +7
INF = 10**9
def main():
    n,m = list(map(int,input().split()))
    V0 = [[] for _ in range(n)]
    V1 = [[] for _ in range(n)]
    V2 = [[] for _ in range(n)]
    for _ in range(m):
        u,v = list(map(int,input().split()))
        u -= 1
        v -= 1
        V0[u].append(v)
        V1[u].append(v)
        V2[u].append(v)
    
    s,t = list(map(int,input().split()))
    s -= 1
    t -= 1
    
    d0 = [-1]*n
    d1 = [-1]*n
    d2 = [-1]*n
    d0[s] = 0

    q = queue.Queue()
    q.put([s,0])
    while q.qsize():
        p = q.get()
        mod = p[1]
        v = p[0]
        if mod == 0:
            for e in V0[v]:
                if d1[e] == -1:
                    d1[e] = d0[v] + 1
                    q.put([e,1])

        elif mod == 1:
            for e in V1[v]:
                if d2[e] == -1:
                    d2[e] = d1[v] + 1
                    q.put([e,2])
        
        else:
            for e in V2[v]:
                if e == t:
                    print(((d2[v] + 1)//3))
                    exit()           
                if d0[e] == -1:
                    d0[e] = d2[v] + 1
                    q.put([e,0])
    
    if d0[t] == -1:
        print((-1))
   
if __name__=='__main__':
    main()


# Optimized solution
from collections import deque 
MOD = 10**9 +7
INF = 10**9
def main():
    n,m = list(map(int,input().split()))
    V0 = [[] for _ in range(n)]
    V1 = [[] for _ in range(n)]
    V2 = [[] for _ in range(n)]
    for _ in range(m):
        u,v = list(map(int,input().split()))
        u -= 1
        v -= 1
        V0[u].append(v)
        V1[u].append(v)
        V2[u].append(v)
    
    s,t = list(map(int,input().split()))
    s -= 1
    t -= 1
    
    d0 = [-1]*n
    d1 = [-1]*n
    d2 = [-1]*n
    d0[s] = 0

    q = deque()
    q.append((s,0))
    while len(q):
        v,mod = q.popleft()
        if mod == 0:
            for e in V0[v]:
                if d1[e] == -1:
                    d1[e] = d0[v] + 1
                    q.append([e,1])

        elif mod == 1:
            for e in V1[v]:
                if d2[e] == -1:
                    d2[e] = d1[v] + 1
                    q.append([e,2])
        
        else:
            for e in V2[v]:
                if e == t:
                    print(((d2[v] + 1)//3))
                    exit()           
                if d0[e] == -1:
                    d0[e] = d2[v] + 1
                    q.append([e,0])
    
    if d0[t] == -1:
        print((-1))
   
if __name__=='__main__':
    main()
# Original solution
from collections import deque

inf = float('inf')

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

es = [[] for _ in range(N * 3)]
# 有向グラフ

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

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

dq = deque([(S, 0)])
dist = [inf] * (N * 3)
dist[S] = 0

while dq:
    v, c = dq.popleft()
    nc = c + 1
    for nv in es[v]:
        if nc < dist[nv]:
            dist[nv] = nc
            if v == T:
                break
            dq.append((nv, nc))

if dist[T] == inf:
    print((-1))
else:
    print((dist[T] // 3))

# print(dist)


# Optimized solution
from collections import deque

inf = float('inf')

n, m = list(map(int, input().split()))

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

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

dq = deque()
dq.append(s)

can = False
dist = [inf] * (n * 3)
dist[s] = 0

while dq:
    v = dq.popleft()
    if v == t:
        can = True
        break
    for nv in es[v]:
        if dist[nv] == inf:
            dq.append(nv)
            dist[nv] = dist[v] + 1

if not can:
    print((-1))
else:
    print((dist[t] // 3))
# Original solution
def bfs(g, N, s, t):
    from collections import deque

    dist = [-1] * (N * 3)
    dist[s] = 0

    dq = deque()
    dq.append(s)

    while dq:
        v = dq.popleft()
        d = dist[v]
        for u in g[v]:
            if ~dist[u]: continue
            dist[u] = d + 1
            if u == t:
                return dist[t] // 3  # TLEしたので解を見つけたら即returnに変えた
            dq.append(u)
    return -1


def main():
    import sys
    input = sys.stdin.readline

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

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

    S, T = (int(x) - 1 for x in input().split())

    ans = bfs(g, N, S, T)

    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    from collections import deque
    import sys
    input = sys.stdin.readline

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

    g = tuple(set() for _ in range(N * 3))
    for _ in range(M):
        u, v = (int(x) - 1 for x in input().split())
        g[u].add(v + N)
        g[u + N].add(v + N + N)
        g[u + N + N].add(v)

    S, T = (int(x) - 1 for x in input().split())

    dist = [-1] * (N * 3)
    dist[S] = 0

    dq = deque()
    dq.append(S)
    while dq:
        v = dq.popleft()
        for u in g[v]:
            if ~dist[u]: continue
            dist[u] = dist[v] + 1
            dq.append(u)

    if dist[T] % 3:
        print((-1))
        return

    print((dist[T] // 3))


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


def main():
    N, M = list(map(int, input().split()))
    edges = [[] for _ in range(N)]

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

    converted_edges = [[] for _ in range(N)]
    for v, v_edge in enumerate(edges):
        three_steps_nodes = []
        for w in edges[v]:
            for x in edges[w]:
                for y in edges[x]:
                    three_steps_nodes.append(y)
        converted_edges[v] = three_steps_nodes[:]

    S, T = [int(x) - 1 for x in input().split()]
    q = deque([(S, 0)])

    nodes = [-1] * N

    while q:
        cv, count = q.popleft()
        if cv == T:
            if nodes[cv] == -1:
                nodes[cv] = count
            else:
                nodes[cv] = min(nodes[cv], count)
        else:
            if nodes[cv] == -1 or count < nodes[cv]:
                nodes[cv] = count
                for u in converted_edges[cv]:
                    q.append((u, count + 1))

    print((nodes[T]))


if __name__ == '__main__':
    main()


# Optimized solution
from collections import deque


def main():
    N, M = list(map(int, input().split()))
    edges = [[] for _ in range(3 * N)]

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

    S, T = [(int(x) - 1) * 3 for x in input().split()]
    q = deque([(S, 0)])
    nodes = [-1] * (3 * N)

    while q:
        cv, count = q.popleft()

        if cv == T:
            nodes[cv] = count if nodes[cv] == -1 else min(nodes[cv], count)
        else:
            if nodes[cv] == -1 or count < nodes[cv]:
                nodes[cv] = count
                for u in edges[cv]:
                    q.append((u, count + 1))

    print((nodes[T] // 3 if nodes[cv] != -1 else -1))


if __name__ == '__main__':
    main()
# Original solution
#coding: utf-8
from collections import deque
N, M = list(map(int, input().split()))
graph = {i:[] for i in range(1, N+1)}
for i in range(M):
    u, v = list(map(int, input().split()))
    graph[u].append(v)
    # graph[v].append(u)
S, T = list(map(int, input().split()))
queue = deque([[graph[S][i], 0] for i in range(len(graph[S]))])
cnt = 0
visited = [False for _ in range(N)]
while queue:
    # print(len(queue))
    now = queue.popleft()
    # print(now)
    now[1] += 1
    # if visited[now[0]-1]:
    #     continue
    if now[1] >= 10**6:
        break
    if now[1] % 3 == 0:
        # print("visited", now[0])
        visited[now[0]-1] = True
        if now[0] == T:
            print((now[1] // 3))
            exit()
        for i in range(len(graph[now[0]])):
            queue.append([graph[now[0]][i], now[1]])
    else:
        for i in range(len(graph[now[0]])):
            queue.append([graph[now[0]][i], now[1]])
print((-1))



# Optimized solution
from sys import stdin
from collections import deque
def main():
    #入力
    readline=stdin.readline
    n,m=list(map(int,readline().split()))
    G=[[] for _ in range(n*3)]
    for i in range(m):
        a,b=[int(x)-1 for x in readline().split()]
        for j in range(3):
            G[3*a+j].append(3*b+(j+1)%3)

    s,t=[int(x)-1 for x in readline().split()]
    s*=3
    t*=3
    #bfs
    d=deque([s])
    flags=[-1]*(3*n)
    flags[s]=0
    while len(d)>0:
        now=d.popleft()
        for nex in G[now]:
            if flags[nex]==-1:
                flags[nex]=flags[now]+1
                d.append(nex)

    print((flags[t]//3))

if __name__=="__main__":
    main()
# Original solution
#!/usr/bin/env python

import sys

def getVertex(i,e):
    v= set()
    for v1 in e[i]:
        for v2 in e[v1]:
            for v3 in e[v2]: v.add( v3 )
    #
    return v
#
n,m = [ int(x) for x in sys.stdin.readline().split() ]
e = { i:[] for i in range(n) }
for _ in range(m):
    u,v = [ int(x) for x in sys.stdin.readline().split() ]
    e[u-1].append( v-1 )
s,t = [ int(x)-1 for x in sys.stdin.readline().split() ]
#
hop= 0
now = {s}
visit = {s}
while True:
    hop+= 1
    n= set()
    for i in now:
        n |= getVertex(i,e) - visit
    #
    if t in n: break
    if not n:
        hop= -1
        break
    now = n
    visit|= n
#
print( hop )


# Optimized solution
#!/usr/bin/env python

import sys

v1mask= set()
v2mask= set()

def getVertex(i,e):
    global v1mask, v2mask
    v= set()
    for v1 in e[i]:
        if v1 in v1mask: continue
        v1mask.add( v1 )
        for v2 in e[v1]:
            if v2 in v2mask: continue
            v2mask.add( v2 )
            for v3 in e[v2]: v.add( v3 )
    #
    return v
#
n,m = [ int(x) for x in sys.stdin.readline().split() ]
e = { i:[] for i in range(n) }
for _ in range(m):
    u,v = [ int(x) for x in sys.stdin.readline().split() ]
    e[u-1].append( v-1 )
s,t = [ int(x)-1 for x in sys.stdin.readline().split() ]
#
hop= 0
now = {s}
visit = {s}
while True:
    hop+= 1
    n= set()
    for i in now:
        n |= getVertex(i,e) - visit
        if t in n: break
    #
    if t in n: break
    if not n:
        hop= -1
        break
    now = n
    visit|= n
#
print( hop )
# Original solution
from collections import deque
n, m = list(map(int, input().split()))
graph = [set([]) for _ in range(n+1)]
for _ in range(m):
    u, v = list(map(int, input().split()))
    graph[u].add(v)


graph3 = [set([]) for _ in range(n+1)]
for i, g in enumerate(graph):
    if i == 0:
        continue
        
    graph2 = set([])
    for r in g:
        graph2 |= graph[r]

    for r in graph2:
        graph3[i] |= graph[r]


s, t = list(map(int, input().split()))
q = deque([s])
visited = [False] * (n+1)
visited[s] = True
dist = [-1] * (n+1)
dist[s] = 0
while q:
    now = q.popleft()
    for i in graph3[now]:
        if visited[i]:
            continue
        if i == now:
            continue
        q.append(i)
        visited[i] = True
        dist[i] = dist[now] + 1

print((dist[t]))


# Optimized solution
from collections import deque
n, m = list(map(int, input().split()))
graph = [[[] for _ in range(3)] for _ in range(n+1)]
for _ in range(m):
    u, v = list(map(int, input().split()))
    graph[u][0].append([v, 1])
    graph[u][1].append([v, 2])
    graph[u][2].append([v, 0])


s, t = list(map(int, input().split()))
q = deque([[s, 0]])
visited = [[False] * 3 for _ in range(n+1)]
visited[s][0] = True
dist = [[-3] * 3 for _ in range(n+1)]
dist[s][0] = 0
while q:
    num, cnt = q.popleft()
    for i, j in graph[num][cnt]:
        if visited[i][j]:
            continue
        q.append([i, j])
        visited[i][j] = True
        dist[i][j] = dist[num][cnt] + 1

print((dist[t][0]//3))
# Original solution
from collections import deque
n, m = list(map(int, input().split()))
graph = [[[] for _ in range(3)] for _ in range(n+1)]
for _ in range(m):
    u, v = list(map(int, input().split()))
    graph[u][0].append([v, 1])
    graph[u][1].append([v, 2])
    graph[u][2].append([v, 0])


s, t = list(map(int, input().split()))
q = deque([[s, 0]])
visited = [[False] * 3 for _ in range(n+1)]
visited[s][0] = True
dist = [[-3] * 3 for _ in range(n+1)]
dist[s][0] = 0
while q:
    num, cnt = q.popleft()
    for i, j in graph[num][cnt]:
        if visited[i][j]:
            continue
        q.append([i, j])
        visited[i][j] = True
        dist[i][j] = dist[num][cnt] + 1

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


# Optimized solution
from collections import deque
n, m = list(map(int, input().split()))

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

s, t = list(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))
# Original solution
import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(eval(input()))
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

N, M = rl()
E = defaultdict(list)
for i in range(M):
	u, v = rl()
	E[u-1].append(v-1)
	#E[v-1].append(u-1)
S, T = rl()
S -= 1
T -= 1

dp = [[float('inf')]*3 for i in range(N)]

def dfs(n, d):
	for m in E[n]:
		r = (d+1)%3
		if dp[m][r] > d+1:
			dp[m][r] = d+1
			dfs(m, d+1)

dfs(S, 0)
if dp[T][0] != float('inf'):
	print((dp[T][0]//3))
else:
	print((-1))


# Optimized solution
import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(eval(input()))
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

N, M = rl()
E = defaultdict(list)
for i in range(M):
	u, v = rl()
	E[u-1].append(v-1)
S, T = rl()
S -= 1
T -= 1

dp = [[float('inf')]*3 for i in range(N)]

heap = []
heappush(heap, (0, S, 0))
while heap:
	d, n, r = heappop(heap)
	if n == T and r == 0:
		continue
	#if dp[n][r] != d:
	#	continue
	nr = (d+1)%3
	for m in E[n]:
		if dp[m][nr] > d+1:
			dp[m][nr] = d+1
			heappush(heap, (d+1, m, nr))

if dp[T][0] != float('inf'):
	print((dp[T][0]//3))
else:
	print((-1))
# Original solution
import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(eval(input()))
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

N, M = rl()
E = defaultdict(list)
for i in range(M):
	u, v = rl()
	E[u-1].append(v-1)
S, T = rl()
S -= 1
T -= 1

dp = [[float('inf')]*3 for i in range(N)]

heap = []
heappush(heap, (0, S, 0))
while heap:
	d, n, r = heappop(heap)
	if n == T and r == 0:
		continue
	#if dp[n][r] != d:
	#	continue
	nr = (d+1)%3
	for m in E[n]:
		if dp[m][nr] > d+1:
			dp[m][nr] = d+1
			heappush(heap, (d+1, m, nr))

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


# Optimized solution
import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(eval(input()))
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

N, M = rl()
E = defaultdict(list)
for i in range(M):
	u, v = rl()
	E[u-1].append(v-1)
S, T = rl()
S -= 1
T -= 1

queue = deque()
queue.append((S,0,0))
visited = set()
while queue:
	n, r, d = queue.popleft()
	if n == T and r == 0:
		print((d//3))
		exit()
	nr = (r+1)%3
	for m in E[n]:
		if (m, nr) in visited: continue
		visited.add((m, nr))
		queue.append((m, nr, d+1))
print((-1))