CodeEff/ECCO huggingface dataset:
# Original solution
from collections import defaultdict, deque


def kenkenpa(s, visited):
    res = []
    for v1 in e[s]:
        if visited[v1][1]:
            continue
        visited[v1][1] = True
        for v2 in e[v1]:
            if visited[v2][2]:
                continue
            visited[v2][2] = True
            for v3 in e[v2]:
                if visited[v3][0]:
                    continue
                visited[v3][0] = True
                res.append(v3)
    return res


N, M = list(map(int, input().split()))
e = defaultdict(list)
for i in range(M):
    u, v = list(map(int, input().split()))
    e[u].append(v)

S, T = list(map(int, input().split()))
q = deque([[S, 0]])
visited = [[False]*3 for _ in range(N+1)]
visited[S][0] = True
ans = -1
while q:
    u, c = q.popleft()
    if u == T:
        ans = c
        break
    l = kenkenpa(u, visited)
    for v in l:
        q.append([v, c+1])
print(ans)


# Optimized solution
from collections import defaultdict, deque

N, M = list(map(int, input().split()))
e = defaultdict(list)
for i in range(M):
    u, v = list(map(int, input().split()))
    e[u].append(v)

'''
頂点を%3で考える(1つの頂点にを3つに分解する)
(u,0)->(v,1)
(v,1)->(u,2)
(u,2)->(v,0)
'''

S, T = list(map(int, input().split()))
q = deque([[S, 0]])
dist = [[-3]*3 for _ in range(N+1)]
dist[S][0] = 0
while q:
    u, c = q.popleft()
    for v in e[u]:
        step = (c+1) % 3
        if dist[v][step] < 0:
            dist[v][step] = c+1
            q.append([v, c+1])

print((dist[T][0]//3))
# Original solution
def f(n, m):
    to1 = {}
    for _ in range(m):
        u, v = list(map(int, input().split()))
        to1.setdefault(u, [])
        to1[u] += [v]
    s, t = list(map(int, input().split()))

    vst1 = set()
    cnt = 0
    to = {}

    for i in range(1, n + 1):
        ss = set([i])
        for _ in range(3):
            nss = []
            for ii in ss:
                if ii not in to1: continue
                nss+=to1[ii]
            ss = set(nss)
        to[i] = list(ss)

#    print(to)

    ss = set([s])
    while 1:
        cnt += 1
#        print(ss)
        nss=set()
        for i in ss:
            if i not in to: continue
            for j in to[i]:
                nss.add(j)
        ss = nss
#        print(ss)
        if t in ss:
            print(cnt)
            exit()
        if ss <= vst1:
            print((-1))
            exit()
        vst1 |= ss

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


# Optimized solution
def f(n, m):
    to = {}
    for _ in range(m):
        u, v = list(map(int, input().split()))
        to.setdefault(u, [])
        to[u] += [v]
    s, t = list(map(int, input().split()))
    vst = [[False] * 3 for _ in range(n + 1)]
    a = [s]
    vst[s][0] = True
    cnt = 0
    while a:
        b = []
        cnt += 1
        j = cnt % 3
        for ak in a:
            if ak not in to: continue
            for i in to[ak]:
                if vst[i][j]: continue
                vst[i][j] = True
                b.append(i)
            if vst[t][0]:
                print((cnt // 3))
                exit()
        a = b
    print((-1))

n, m = list(map(int, input().split()))
f(n, m)
# Original solution
#
import itertools
from collections import Counter
from collections import defaultdict
import bisect
from heapq import heappush, heappop

def next_kenp(node, edges):
    nodes = set()
    nodes2 = set()
    for n1 in edges[node]:
        for n2 in edges[n1]:
            if n2 not in nodes2:
                nodes2.add(n2)
    for n2 in nodes2:
        for n3 in edges[n2]:
            nodes.add(n3)
    return nodes


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

    edges = defaultdict(list)

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

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

    # print(new_edges)

    dones = set()
    que = [((s, 0), 0)]
    while que:
        node, step = que.pop(0)

        next_nodes = edges[node]
        # print(next_nodes)
        for n, l in next_nodes:
            if n == t and l == 0:
                print(((step + 1) // 3))
                exit()

            if (n, l) not in dones:
                que.append(((n, l), step + 1))
                dones.add((n, l))
    print((-1))


if __name__ == '__main__':
    main()


# Optimized solution
#
import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop


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

    edges = defaultdict(list)

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

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

    # print(new_edges)

    dones = set()
    que = deque()
    que.append(((s, 0), 0))
    while que:
        node, step = que.popleft()

        next_nodes = edges[node]
        # print(next_nodes)
        for n, l in next_nodes:
            if n == t and l == 0:
                print(((step + 1) // 3))
                exit()

            if (n, l) not in dones:
                que.append(((n, l), step + 1))
                dones.add((n, l))
    print((-1))


if __name__ == '__main__':
    main()
# Original solution
from collections import deque
import sys
input = sys.stdin.readline


def reachable_by_3steps_from(node, edges):
    dist1 = edges[node]
    reachable = []
    for d1 in dist1:
        dist2 = edges[d1]
        for d2 in dist2:
            reachable += edges[d2]
    return reachable


n, m = list(map(int, input().split()))
edges = [[] for _ in range(n)]
for _ in range(m):
    _from, to = list(map(int, input().split()))
    edges[_from-1].append(to-1)

start, goal = list(map(int, input().split()))
skip_path = [[] for _ in range(n)]
for i in range(n):
    skip_path[i] = reachable_by_3steps_from(i, edges)

todo = deque([])
seen = [False] * n
seen[start-1] = True
for to in skip_path[start-1]:
    todo.append([to, 1])
    if to == goal-1:
        print((1))
        exit()

while todo:
    node, dist = todo.popleft()
    if seen[node]:
        continue
    seen[node] = True
    for to in skip_path[node]:
        if seen[to]:
            continue
        todo.append([to, dist+1])
        if to == goal-1:
            print((dist+1))
            exit()

print((-1))

# Optimized solution
from collections import deque
import sys
input = sys.stdin.readline


n, m = list(map(int, input().split()))
edges = [[] for _ in range(n)]
for _ in range(m):
    _from, to = list(map(int, input().split()))
    edges[_from-1].append(to-1)

start, goal = list(map(int, input().split()))

todo = deque([])
seen = [[False] * n for i in range(3)]
seen[0][start-1] = True
for to in edges[start-1]:
    dim = 1
    dist = 1
    todo.append([to, dim, dist])

while todo:
    node, dim, dist = todo.popleft()
    if seen[dim][node]:
        continue
    seen[dim][node] = True
    dim += 1
    if dim == 3:
        dim = 0
    for to in edges[node]:
        if seen[dim][to]:
            continue
        todo.append([to, dim, dist+1])
        if to == goal-1 and dim == 0:
            print(((dist+1)//3))
            exit()

print((-1))
# Original solution
import os
import sys
from collections import defaultdict

from functools import lru_cache

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18

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

graph = defaultdict(set)
for u, v in UV:
    graph[u].add(v)

memo = [[None] * (N+1) for _ in range(3)]


def dest(v, count):
    if memo[count - 1][v] is not None:
        return memo[count - 1][v]

    if count <= 1:
        memo[count - 1][v] = graph[v]
        return graph[v]
    ret = set()
    for u in graph[v]:
        ret |= dest(u, count - 1)
    memo[count - 1][v] = ret
    return ret


checked = set()
vs = {S}
i = 0
prev = 0
while True:
    i += 1
    s = set()
    for v in vs:
        s |= dest(v, 3)
    checked |= vs
    if T in s:
        print(i)
        break
    if len(checked) == prev:
        print((-1))
        break
    vs = s - checked
    prev = len(checked)


# Optimized solution
import os
import sys
from collections import defaultdict

from functools import lru_cache

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18

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

graph = defaultdict(set)
for u, v in UV:
    graph[u].add(v)

vs = {S}
checked = set()
i = 0
prev = 0
while True:
    i += 1
    checked |= vs
    for _ in range(3):
        ss = set()
        for v in vs:
            ss |= graph[v]
        vs = ss
    if T in vs:
        print(i)
        break
    if len(checked) == prev:
        print((-1))
        break
    prev = len(checked)
# Original solution
import heapq
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18

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

U = []
V = []
for u, v in UV:
    U.extend([u, u + N, u + N * 2])
    V.extend([v + N, v + N * 2, v])

# graph = csr_matrix(([1] * (M * 3), (U, V)), shape=(N * 3 + 1, N * 3 + 1))
# dist = dijkstra(graph, indices=S)[T]
# if dist < np.inf:
#     print(int(dist // 3))
# else:
#     print(-1)

#: :type: list of (list of int)
graph = [[] for _ in range(N * 3 + 1)]
for u, v in zip(U, V):
    graph[u].append(v)

# S から T までの距離
heap = [(0, S)]
dist = [IINF] * (N * 3 + 1)
while heap:
    d, v = heapq.heappop(heap)
    if v == T:
        print((int(d // 3)))
        exit()
    for u in graph[v]:
        if d + 1 < dist[u]:
            dist[u] = d + 1
            heapq.heappush(heap, (d + 1, u))
print((-1))


# Optimized solution
import heapq
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18

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

#: :type: list of (list of int)
graph = [[] for _ in range(N * 3 + 1)]
for v, u in VU:
    graph[v].append(u + N)
    graph[v + N].append(u + N * 2)
    graph[v + N * 2].append(u)

heap = [(0, S)]
dist = [INF] * (N * 3 + 1)
ans = -1
while heap:
    d, v = heapq.heappop(heap)
    if v == T and d % 3 == 0:
        ans = int(d // 3)
        break
    for u in graph[v]:
        if d + 1 < dist[u]:
            dist[u] = d + 1
            heapq.heappush(heap, (d + 1, u))
print(ans)
# Original solution
def dijkstra(edge,start,end) :
    import heapq

    minCosts = [float('inf') for _ in range(len(edge))]

    nodeHeap = []
    heapq.heappush(nodeHeap, (0, start))  # (cost, node)

    while nodeHeap :
        cost, node = heapq.heappop(nodeHeap)

        if minCosts[node] <= cost :
            continue

        minCosts[node] = cost

        for nextNode , weight in edge[node] :
            newCost = cost + weight
            heapq.heappush(nodeHeap, (newCost, nextNode))
    dist = minCosts[endNode]
    if dist == float('inf') :
        return -1
    return dist

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

# edges , costs
nodes = [[] for _ in range(3 * N)]

# 辺の入力
for _ in range(M) :
    fromNode , toNode = list(map(int,input().split()))
    fromNode -= 1
    toNode -= 1
    fromNode *= 3
    toNode *= 3

    nodes[fromNode].append((toNode + 1,1))
    nodes[fromNode + 1].append((toNode + 2,1))
    nodes[fromNode + 2].append((toNode,1))

# スタート ゴール
startNode , endNode = list(map(int,input().split()))
startNode -= 1
endNode -= 1
startNode *= 3
endNode *= 3

ans = dijkstra(nodes,startNode,endNode)

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

# Optimized solution
import heapq
 
N , M = list(map(int,input().split()))
 
# edges , costs
nodes = [[] for _ in range(3 * N)]
 
confilm = [False] * (3 * N)  # 確定フラグ
 
costs = [-1] * (3 * N)  # 最短経路
 
# 辺の入力
for _ in range(M) :
    fromNode , toNode = list(map(int,input().split()))
    fromNode -= 1
    toNode -= 1
    fromNode *= 3
    toNode *= 3
 
    nodes[fromNode].append((toNode + 1,1))
    nodes[fromNode + 1].append((toNode + 2,1))
    nodes[fromNode + 2].append((toNode,1))
 
 
# スタート ゴール
startNode , endNode = list(map(int,input().split()))
startNode -= 1
endNode -= 1

startNode *= 3
endNode *= 3
 
costs[startNode] = 0
 
q = [(0,startNode)]
 
# 探索
while q : # 空になるまで
    nowCost , nowNode = heapq.heappop(q)
 
    if confilm[nowNode] :
        continue
 
    confilm[nowNode] = True
 
    # 長さを更新
    for nextNode , cost in nodes[nowNode] :
        # if not confilm[nextNode] :  # 確定ノード以外
        nextCost = nowCost + cost
        if costs[nextNode] == -1 or nextCost < costs[nextNode] :
            costs[nextNode] = nextCost  # コストを更新
            heapq.heappush(q,(nextCost,nextNode))
 
if costs[endNode] < 0 :
    print((-1))
else :
    print((costs[endNode] // 3))
# Original solution
import sys,atexit
from io import BytesIO
inp = BytesIO(sys.stdin.buffer.read())
input = lambda:inp.readline().decode('ascii')
buf = BytesIO()
sys.stdout.write = lambda s: buf.write(s.encode('ascii'))
atexit.register(lambda:sys.__stdout__.buffer.write(buf.getvalue()))

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

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

for _ in range(m):
    a,b = [int(x)-1 for x in input().split()]
    C[a].append(b)

start,end = [int(x)-1 for x in input().split()]

D = [(i,nei) for i in range(n) for nei in C[i]]
D = [(i,newnei) for i,nei in D for newnei in C[nei]]
D = [(i,newnei) for i,nei in D for newnei in C[nei]]

C = [set() for _ in range(n)]
for a,b in D:
    C[a].add(b)

from collections import defaultdict as dd, deque

res = -1
Q = deque()
Q.append((start,0))
vis = [0]*n
while Q:
    cur,dist = Q.popleft()

    if cur == end:
        res = dist
        break

    if vis[cur]: continue
    vis[cur] = 1

    for nei in C[cur]:
        Q.append((nei,dist+1))

print(res)


# Optimized solution
import sys,atexit
from io import BytesIO
inp = BytesIO(sys.stdin.buffer.read())
input = lambda:inp.readline().decode('ascii')
buf = BytesIO()
sys.stdout.write = lambda s: buf.write(s.encode('ascii'))
atexit.register(lambda:sys.__stdout__.buffer.write(buf.getvalue()))

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

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

for _ in range(m):
    a,b = [int(x)-1 for x in input().split()]
    C[a].append(b)

start,end = [int(x)-1 for x in input().split()]

D = [(i,nei) for i in range(n) for nei in C[i]]
D = {(i,newnei) for i,nei in D for newnei in C[nei]}
D = {(i,newnei) for i,nei in D for newnei in C[nei]}

C = [[] for _ in range(n)]
for a,b in D:
    C[a].append(b)

from collections import defaultdict as dd, deque

res = -1
Q = deque()
Q.append((start,0))
vis = [0]*n
while Q:
    cur,dist = Q.popleft()

    if cur == end:
        res = dist
        break

    if vis[cur]: continue
    vis[cur] = 1

    for nei in C[cur]:
        Q.append((nei,dist+1))

print(res)
# Original solution
import heapq


class Dijkstra:

    def __init__(self, rote_map, start_point, goal_point=None):
        self.rote_map = rote_map
        self.start_point = start_point
        self.goal_point = goal_point

    def execute(self):
        num_of_city = 3 * 10 ** 5 + 10
        dist = [float("inf") for _ in range(num_of_city)]
        prev = [float("inf") for _ in range(num_of_city)]

        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        route_count = [0 for _ in range(num_of_city)]
        route_count[self.start_point] = 1
        while len(heap_q) != 0:
            prev_cost, src = heapq.heappop(heap_q)

            if dist[src] < prev_cost:
                continue

            for dest, cost in list(self.rote_map[src].items()):
                if cost != float("inf") and dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
                    prev[dest] = src
                if dist[dest] == dist[src] + cost:
                    route_count[dest] += route_count[src]

        if self.goal_point is not None:
            return self._get_path(self.goal_point, prev)
        else:
            return dist, route_count

    def _get_path(self, goal, prev):
        path = [goal]
        dest = goal

        while prev[dest] != float("inf"):
            path.append(prev[dest])
            dest = prev[dest]

        return list(reversed(path))


N, M = list(map(int, input().split()))
from collections import defaultdict

graph = defaultdict(dict)
for i in range(M):
    u, v = list(map(int, input().split()))
    u -= 1
    v -= 1
    graph[u][v + N] = 1
    graph[u + N][v + 2 * N] = 1
    graph[u + 2 * N][v] = 1
S, T = list(map(int, input().split()))
S -= 1
T -= 1
dj = Dijkstra(graph, S)

dist, _ = dj.execute()
if dist[T] % 3 == 0:
    print((dist[T] // 3))
else:
    print((-1))


# Optimized solution
N, M = list(map(int, input().split()))
from collections import defaultdict, deque

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

dist = [-1] * (3*N)
dist[S] = 0
q = deque([S])
while q:
    v = q.popleft()
    d = dist[v]
    Vs = graph[v]
    for u in Vs:
        if dist[u] == -1:
            q.append(u)
            dist[u] = d + 1
if dist[T] != float("inf") and dist[T] % 3 == 0:
    print((dist[T] // 3))
else:
    print((-1))
# Original solution
N, M = list(map(int, input().split()))
from collections import defaultdict, deque

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

dist = [-1] * (3*N)
dist[S] = 0
q = deque([S])
while q:
    v = q.popleft()
    d = dist[v]
    Vs = graph[v]
    for u in Vs:
        if dist[u] == -1:
            q.append(u)
            dist[u] = d + 1
if dist[T] != float("inf") and dist[T] % 3 == 0:
    print((dist[T] // 3))
else:
    print((-1))


# Optimized solution
N, M = list(map(int, input().split()))
from collections import defaultdict, deque

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

dist = [-1] * (3*N)
dist[S] = 0
que = deque([S])
while len(que) != 0:
    q = que.popleft()
    cost = dist[q]
    for v in graph[q]:
        if dist[v] == -1:
            dist[v] = cost + 1
            que.append(v)
if dist[T] % 3 == 0:
    print((dist[T] // 3))
else:
    print((-1))
# Original solution
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(eval(input()))
s = lambda: eval(input())

N, M = nl()

from collections import defaultdict
edges = defaultdict(set)
for i in range(M):
    u, v = nl()
    edges[u].add(v+N)
    edges[u+N].add(v+2*N)
    edges[u+2*N].add(v)

S, T = nl()

import queue
q = queue.Queue()
q.put((S, 0))

done = defaultdict(bool)
done[S] = True

while not q.empty():
    src, cost = q.get()
    for dst in edges[src]:
        if dst == T:
            print(((cost+1) // 3))
            import sys
            sys.exit()

        if not done[dst]:
            done[dst] = True
            q.put((dst, cost+1))

print((-1))




# Optimized solution
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(eval(input()))
s = lambda: eval(input())

N, M = nl()

from collections import defaultdict
edges = defaultdict(set)
for i in range(M):
    u, v = nl()
    edges[u].add(v+N)
    edges[u+N].add(v+2*N)
    edges[u+2*N].add(v)

S, T = nl()

import queue
q = queue.Queue()
q.put((S, 0))

done = [False] * (3*N+1)
done[S] = True

while not q.empty():
    src, cost = q.get()
    for dst in edges[src]:
        if dst == T:
            print(((cost+1) // 3))
            import sys
            sys.exit()

        if not done[dst]:
            done[dst] = True
            q.put((dst, cost+1))

print((-1))
# Original solution
n, m = list(map(int, input().split()))
uv = [list(map(int, input().split())) for _ in range(m)]
s, t = list(map(int, input().split()))

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

s -= 1
t -= 1

f = False
visited = set([s])
q = deque([(s, 0)])
while q:
    cur, d = q.popleft()

    if cur == t:
        f = True
        break
    for target1 in g1[cur]:
        for target2 in g1[target1]:
            for target3 in g1[target2]:
                if target3 not in visited:
                    q.append((target3, d + 1))

if f:
    print(d)
else:
    print((-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()))

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))
# Original solution
from collections import defaultdict
N, M = list(map(int, input().split()))
dic = defaultdict(list)
for i in range(M):
  u, v = list(map(int, input().split()))
  dic[u-1] += [v-1]
S, T = list(map(int, input().split()))
S -= 1
T -= 1
dist = [[float('inf')]*3 for i in range(N)]
ls = [float('inf')]*3
dist[S][0] = 0
q = [(S,0)]
flag = False
while q:
  e, m = q.pop()
  n = (m+1)%3
  for p in dic[e]:
    if p==T:
      flag = True
    if dist[p][n]>dist[e][m]+1:
      dist[p][n] = dist[e][m]+1
      q += [(p,n)]
if flag:
  ans = dist[T][0] if dist[T][0]!=float('inf') else -1
  print((ans//3))
else:
  print((-1))
      

# Optimized solution
from collections import defaultdict, deque
N, M = list(map(int, input().split()))
dic = defaultdict(list)
for i in range(M):
  u, v = list(map(int, input().split()))
  dic[u-1] += [v-1]
S, T = list(map(int, input().split()))
S -= 1
T -= 1
dist = [[float('inf')]*3 for i in range(N)]
dist[S][0] = 0
q = deque([(S,0)])
flag = False
cnt = 0
while q:
  cnt += 1
  e, m = q.popleft()
  n = (m+1)%3
  for p in dic[e]:
    if p==T:
      flag = True
    if dist[p][n]>dist[e][m]+1:
      dist[p][n] = dist[e][m]+1
      q += [(p,n)]
if flag:
  ans = dist[T][0] if dist[T][0]!=float('inf') else -1
  print((ans//3))
else:
  print((-1))