CodeEff/ECCO huggingface dataset:
# Original solution
import sys
from collections import Counter, deque, defaultdict
from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
from functools import reduce
from math import ceil, floor, log10, factorial
import math

sys.setrecursionlimit(1000000)

MOD = 10 ** 9 + 7

INF = float('inf')
# N = int(input())
# A = [int(x) for x in input().split()]
# A = [int(input()) for _ in range(N)]
# dp = [[0] * 100 for _ in range(100)]
# dp = defaultdict(lambda: float('inf'))
# print('hoge', file=sys.stderr)

N, M = [int(x) for x in input().split()]

# E = {set() for _ in range(N + 1)}
E = defaultdict(lambda: set())

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

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


def bfs(e, start, end):
    open = {start}
    w = dict()
    w[start] = 0
    while len(open) != 0:
        target = min(open, key=lambda x: w[x])
        open.remove(target)
        for next in E[target]:
            if not next in w:
                open.add(next)
                w[next] = w[target] + 1

    return w[end] // 3 if end in w else -1


print((bfs(E, S, T)))


# Optimized solution
import sys
from collections import Counter, deque, defaultdict
from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
from functools import reduce
from math import ceil, floor, log10, factorial
import math

sys.setrecursionlimit(1000000)

MOD = 10 ** 9 + 7

INF = float('inf')
# N = int(input())
# A = [int(x) for x in input().split()]
# A = [int(input()) for _ in range(N)]
# dp = [[0] * 100 for _ in range(100)]
# dp = defaultdict(lambda: float('inf'))
# print('hoge', file=sys.stderr)

N, M = [int(x) for x in input().split()]

# E = {set() for _ in range(N + 1)}
E = defaultdict(lambda: set())

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

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


def bfs(e, start, end):
    open = deque([start])
    w = dict()
    w[start] = 0
    while len(open) != 0:
        target = open.popleft()
        for next in E[target]:
            if not next in w:
                open.append(next)
                w[next] = w[target] + 1

    return w[end] // 3 if end in w else -1


print((bfs(E, S, T)))
# Original solution
import sys, collections

def solve():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    son = [[] for i in range(N)]
    for i in range(M):
        u, v = list(map(int, input().split()))
        son[u-1].append(v-1)
    S, T = list(map(int, input().split()))

    graphB = [set() for i in range(N)]
    q = collections.deque()
    for i in range(N):
        q.append((0, i))
        pathi = set()
        while q:
            cost, now = q.popleft()
            if cost <= 1:
                for s in son[now]: q.append((cost+1, s))
            elif cost == 2: 
                for s in son[now]: pathi |= {s}
        graphB[i] = pathi
    
    visited = [-1] * N
    q.append((0, S-1))
    while q:
        cost, now = q.popleft()
        if visited[now] < 0:
            visited[now] = cost
            for s in graphB[now]:
                if visited[s] < 0: q.append((cost + 1, s))

    print((visited[T-1]))
    return 0

if __name__ == "__main__":
    solve()

# Optimized solution
import sys, collections

def solve():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    son = [[] for i in range(N)]
    for i in range(M):
        u, v = list(map(int, input().split()))
        son[u-1].append(v-1)
    S, T = list(map(int, input().split()))

    graphB = [[] for i in range(N)] 
    for i in range(N):
        for s1 in son[i]:
            for s2 in son[s1]:
                for s3 in son[s2]: graphB[i].append(s3)
    
    q = collections.deque()
    visited = [-1] * N
    q.append((0, S-1))
    while q:
        cost, now = q.popleft()
        if visited[now] == -1:
            visited[now] = cost
            for s in graphB[now]:
                if visited[s] == -1: q.append((cost + 1, s))

    print((visited[T-1]))
    return 0

if __name__ == "__main__":
    solve()
# Original solution
import sys, collections

def solve():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    son = [[] for i in range(N)]
    for i in range(M):
        u, v = list(map(int, input().split()))
        son[u-1].append(v-1)
    S, T = list(map(int, input().split()))

    graphB = [set() for i in range(N)] 
    for i in range(N):
        for s1 in son[i]:
            for s2 in son[s1]:
                graphB[i] |= set(son[s2])
    
    q = collections.deque()
    visited = [-1] * N
    q.append((0, S-1))
    while q:
        cost, now = q.popleft()
        if visited[now] == -1:
            visited[now] = cost
            for s in graphB[now]:
                if visited[s] == -1: q.append((cost + 1, s))

    print((visited[T-1]))
    return 0

if __name__ == "__main__":
    solve()

# Optimized solution
import sys, collections

def solve():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    son = [[] for i in range(N)] 
    for i in range(M):
        u, v = list(map(int, input().split()))
        son[u-1].append(v-1)
    S, T = list(map(int, input().split()))

    dist = [[-1, -1, -1] for i in range(N)]
    que = collections.deque()
    que.append((0, S-1, 0)) #k回目の移動, 場所, mod
    while que:
        cost, now, mod = que.popleft()
        if dist[now][mod] == -1:
            dist[now][mod] = cost
            if mod == 0:
                for s in son[now]:
                    if dist[s][1] == -1: que.append((cost + 1, s, 1))
            else:
                for s in son[now]:
                    if dist[s][(mod + 1) % 3] == -1: que.append((cost, s, (mod + 1) % 3))

    print((dist[T-1][0]))  
    return 0

if __name__ == "__main__":
    solve()
# Original solution
n,m=list(map(int,input().split()))
visited = [[False,False,False] for _ in range(n)]
ren = [[0]*n for _ in range(n)]
for i in range(m):
    u,v = list(map(int,input().split()))
    ren[u-1][v-1] = 1
s,t = list(map(int,input().split()))

Q = [(s,0)]
visited[s-1][2]=True
ans = -1

while len(Q)>0:
    curs,curcount = Q.pop(0)
    if curs == t:
        ans = curcount
        break
    for i in range(n):
        if ren[curs-1][i] and not visited[i][0]:
            visited[i][0]=True
            for j in range(n):
                if ren[i][j] and not visited[j][1]:
                    visited[j][1]=True
                    for k in range(n):
                        if ren[j][k] and not visited[k][2]:
                            visited[k][2]=True
                            Q.append((k+1,curcount+1))

print(ans)

# Optimized solution
n,m=list(map(int,input().split()))
visited = [[False,False,False] for _ in range(n)]
ren = [[] for _ in range(n)]
for i in range(m):
    u,v = list(map(int,input().split()))
    ren[u-1].append(v)
s,t = list(map(int,input().split()))

Q = [(s,0)]
visited[s-1][2]=True
ans = -1

while len(Q)>0:
    curs,curcount = Q.pop(0)
    if curs == t:
        ans = curcount
        break
    for i in ren[curs-1]:
        if not visited[i-1][0]:
            visited[i-1][0]=True
            for j in ren[i-1]:
                if not visited[j-1][1]:
                    visited[j-1][1]=True
                    for k in ren[j-1]:
                        if not visited[k-1][2]:
                            visited[k-1][2]=True
                            Q.append((k,curcount+1))

print(ans)
# Original solution
import copy
import sys
input = sys.stdin.readline

n, m = list(map(int,input().split()))
edge = []
graph = [[] for i in range(n+1)]
for i in range(m):
    edge.append(list(map(int,input().split())))
    graph[edge[-1][0]].append(edge[-1][1])
s, g = list(map(int,input().split()))

INF = 10**18
ans = [[INF, INF, INF] for i in range(n+1)]
q = [s]
d = 0
while q:
    nq = []
    d += 1
    p = d % 3
    for cf in q:
        for ct in graph[cf]:
            if ans[ct][p] == INF:
                ans[ct][p] = d
                nq.append(ct)
    q = copy.deepcopy(nq)
if ans[g][0] == INF:
    print((-1))
else:
    print((ans[g][0]//3))

# Optimized solution
import copy
import sys
input = sys.stdin.readline

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, g = list(map(int,input().split()))

INF = 10**7
ans = [[INF, INF, INF] for i in range(n+1)]
q = [s]
d = 0
while q:
    nq = []
    d += 1
    p = d % 3
    for cf in q:
        for ct in graph[cf]:
            if ans[ct][p] == INF:
                ans[ct][p] = d
                nq.append(ct)
    q = copy.deepcopy(nq)
if ans[g][0] == INF:
    print((-1))
else:
    print((ans[g][0]//3))
# Original solution
import copy
import sys
input = sys.stdin.readline

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, g = list(map(int,input().split()))

INF = 10**7
ans = [[INF, INF, INF] for i in range(n+1)]
q = [s]
d = 0
while q:
    nq = []
    d += 1
    p = d % 3
    for cf in q:
        for ct in graph[cf]:
            if ans[ct][p] == INF:
                ans[ct][p] = d
                nq.append(ct)
    q = copy.deepcopy(nq)
if ans[g][0] == INF:
    print((-1))
else:
    print((ans[g][0]//3))

# Optimized solution
# 頂点を3回ずつに持つ (x歩目の着地）
# 最短路

import sys
input = sys.stdin.readline
N,M = list(map(int,input().split()))
if M == 0:
  print((-1))
  exit()
edge = [[] for _ in range(N+1)]
for _ in range(M):
  u,v = list(map(int,input().split()))
  edge[u].append(v)
S,T = list(map(int,input().split()))

INF = 10 ** 9
dist = [[INF,INF,INF] for _ in range(N+1)]

d = 0
q = [S]
while q:
  d += 1
  r = d % 3
  qq = []
  for u in q:
    for v in edge[u]:
      if dist[v][r] == INF:
        dist[v][r] = d
        qq.append(v)
  q = qq

d = dist[T][0]
answer = -1 if d == INF else d // 3
print(answer)
# Original solution
import sys
sys.setrecursionlimit(100000)

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

if 0 == M:
	print(-1)
	exit()

s, t = S - 1, T - 1

graph = [[] for _ in range(N)]
for u, v in UV:
	graph[u - 1] += [v - 1]

INF = 10 ** 12
cost = [[INF, INF, INF] for _ in range(N)]
def print_cost(cost):
	for line in cost:
		for cell in line:
			print("  INF" if cell == INF else " {:4d}".format(cell), end="")
		print()
	print()

def dfs(graph, cost, c, d):
	if cost[c][d % 3] <= d:
		return
	cost[c][d % 3] = d
	#print_cost(cost)
	for n in graph[c]:
		dfs(graph, cost, n, d + 1)

dfs(graph, cost, s, 0)

if INF == cost[t][0]:
	print(-1)
else:
	print(cost[t][0] // 3)


# 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()))

if 0 == M:
	print((-1))
	exit()

graph = [set() for _ in range(N)]
for u, v in UV:
	graph[u - 1] |= {v - 1}

INF = 10 ** 12
depths = [[INF, INF, INF] for _ in range(N)]

def bfs(start, end):
	depth = 0
	nodes = {start}
	while nodes:
		if (0 == depth % 3) and (end in nodes):
			return depth // 3
		next_nodes = set()
		for node in nodes:
			if depths[node][depth % 3] <= depth:
				continue
			depths[node][depth % 3] = depth
			next_nodes |= graph[node]
		nodes = next_nodes
		depth += 1
	return -1

print((bfs(S - 1, T - 1)))
# Original solution
import sys
from collections import deque
sys.setrecursionlimit(10**6)

def main():
    input = sys.stdin.readline
    N, M = list(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 = list(map(int, input().split()))
    S, T = S-1, T-1

    q = deque()
    q.append(S)
    cnt = 0
    cnt_k = 0
    visited = [False] * N
    visited[S] = True
    while len(q) > 0:
        cnt += 1
        if cnt % 3 == 0:
            cnt_k += 1

        next_ = set()
        while len(q) > 0:
            n0 = q.pop()
            for n1 in G[n0]:
                if cnt % 3 == 0:
                    if n1 == T:
                        return cnt_k

                    if visited[n1]:
                        continue

                    visited[n1] = True

                next_.add(n1)

        q = deque()
        q.extend(list(next_))

    return -1


if __name__ == '__main__':
    print((main()))


# Optimized solution
import sys
from collections import deque

def main():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    G = [[] for _ in range(3*N)]
    for _ in range(M):
        u, v = list(map(int, input().split()))
        u, v = u-1, v-1

        G[3*u].append(3*v+1)
        G[3*u+1].append(3*v+2)
        G[3*u+2].append(3*v)

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

    cnt = 0
    q = deque()
    q.append(S)
    visited = [False] * (3*N)
    visited[S] = True
    while len(q) > 0:
        q_ = deque()
        cnt += 1
        while len(q) > 0:
            n = q.pop()
            for m in G[n]:
                if visited[m]:
                    continue

                if m == T:
                    return cnt // 3

                visited[m] = True
                q_.append(m)

        q = q_

    return -1


if __name__ == '__main__':
    print((main()))
# Original solution
import sys
sys.setrecursionlimit(200000)
def main():
    INF = float("inf")
    input = sys.stdin.readline
    N, M = list(map(int,input().split()))
    G = [[[],[],[]] for k in range(N)]

    for k in range(M):
        u, v = list(map(int,input().split()))
        u -= 1
        v -= 1
        G[u][0] += [v]
        G[u][1] += [v]
        G[u][2] += [v]

    S, T = list(map(int,input().split()))
    S -= 1
    T -= 1
    D1 = [[INF,INF,INF] for k in range(N)]
    D1[S][0] = 0

    def dfs1(now):
        for tsugi in G[now][1]:
            if D1[tsugi][1] > D1[now][0] + 1:
                D1[tsugi][1] = D1[now][0] + 1
                dfs2(tsugi)
    def dfs2(now):
        for tsugi in G[now][2]:
            if D1[tsugi][2] > D1[now][1] + 1:
                D1[tsugi][2] = D1[now][1] + 1
                dfs3(tsugi)
    def dfs3(now):
        for tsugi in G[now][2]:
            if D1[tsugi][0] > D1[now][2] + 1:
                D1[tsugi][0] = D1[now][2] + 1
                dfs1(tsugi)

    dfs1(S)
    if D1[T][0]%3 == 0:
        print((D1[T][0]//3))
    else:
        print((-1))
if __name__ == '__main__':
    main()


# Optimized solution
import sys

def main():
    INF = float("inf")
    input = sys.stdin.readline
    N, M = list(map(int,input().split()))
    G = [[] for k in range(N)]
    for k in range(M):
        u, v = list(map(int,input().split()))
        u -= 1
        v -= 1
        G[u] += [v]
    S, T = list(map(int,input().split()))
    S -= 1
    T -= 1
    D = [[INF,INF,INF] for k in range(N)]
    D[S][0] = 0
    Q = [[S,0]]
    while len(Q) > 0:
        ima = Q[0][0]
        k = Q[0][1]
        Q.pop(0)
        for tsugi in G[ima]:
            if D[tsugi][(k+1)%3] != INF:
                pass
            else:
                D[tsugi][(k+1)%3] = D[ima][k] + 1
                Q.append([tsugi,(k+1)%3])
    if D[T][0] == INF or D[T][0]%3 != 0:
        print((-1))
    else:
        print((D[T][0]//3))

if __name__ == '__main__':
    main()
# Original solution
from collections import defaultdict
from queue import Queue
n, m = list(map(int, input().split()))
MX = int(1e6)
edge = defaultdict(set)
edge2 = defaultdict(set)
kenedge = defaultdict(set)
dist = [MX for i in range(n)]
reached = [False for i in range(n)]

for loop in range(m):
    a, b = list(map(int, input().split()))
    edge[a-1].add(b-1)

for i in range(n):
    step1 = set()
    for ii in edge[i]:
        edge2[i] |= edge[ii]

for i in range(n):
    step2 = set()
    for ii in edge2[i]:
        kenedge[i] |= edge[ii]

s, t = list(map(int, input().split()))
s = s-1
t = t-1
dist[s] = 0

q = Queue()
q.put(s)
while not q.empty():
    node = q.get()
    reached[node] = True
    for next_node in kenedge[node]:
        if not reached[next_node]: q.put(next_node)
        dist[next_node] = min(dist[next_node], dist[node] + 1)

if dist[t] == MX:
    print((-1))
else:
    print((dist[t]))



# Optimized solution
from collections import defaultdict
from queue import Queue

n, m = list(map(int, input().split()))
MX = int(1e6)
edge = defaultdict(set)
dist = [[MX, MX, MX] for i in range(n)]
reached = [[False, False, False] for i in range(n)]

for loop in range(m):
    a, b = list(map(int, input().split()))
    edge[a-1].add(b-1)

s, t = list(map(int, input().split()))
s = s-1
t = t-1
dist[s][0] = 0

q = Queue()
q.put((s, 0))
while not q.empty():
    node, step = q.get()
    next_step = (step + 1) % 3
    reached[node][step] = True
    for next_node in edge[node]:
        if not reached[next_node][next_step]: q.put((next_node, next_step))
        if step == 2:
            dist[next_node][next_step] = min(dist[next_node][next_step], dist[node][step] + 1)
        else:
            dist[next_node][next_step] = min(dist[next_node][next_step], dist[node][step])

if dist[t][0] == MX:
    print((-1))
else:
    print((dist[t][0]))
# Original solution
from collections import defaultdict
from queue import Queue

n, m = list(map(int, input().split()))
MX = int(1e6)
edge = defaultdict(set)
dist = [[MX, MX, MX] for i in range(n)]
reached = [[False, False, False] for i in range(n)]

for loop in range(m):
    a, b = list(map(int, input().split()))
    edge[a-1].add(b-1)

s, t = list(map(int, input().split()))
s = s-1
t = t-1
dist[s][0] = 0

q = Queue()
q.put((s, 0))
while not q.empty():
    node, step = q.get()
    next_step = (step + 1) % 3
    reached[node][step] = True
    for next_node in edge[node]:
        if reached[next_node][next_step]: continue
        q.put((next_node, next_step))
        if step == 2:
            dist[next_node][next_step] = min(dist[next_node][next_step], dist[node][step] + 1)
        else:
            dist[next_node][next_step] = min(dist[next_node][next_step], dist[node][step])

if dist[t][0] == MX:
    print((-1))
else:
    print((dist[t][0]))



# Optimized solution
from collections import defaultdict
from queue import Queue

n, m = list(map(int, input().split()))
MX = int(1e6)
edge = defaultdict(set)
dist = [[MX, MX, MX] for i in range(n)]
reached = [[False, False, False] for i in range(n)]

for loop in range(m):
    a, b = list(map(int, input().split()))
    edge[a-1].add(b-1)

s, t = list(map(int, input().split()))
s = s-1
t = t-1
dist[s][0] = 0

q = Queue()
q.put((s, 0))
while not q.empty():
    node, step = q.get()
    next_step = (step + 1) % 3
    for next_node in edge[node]:
        if reached[next_node][next_step]: continue
        q.put((next_node, next_step))
        reached[next_node][next_step] = True
        if step == 2:
            dist[next_node][next_step] = min(dist[next_node][next_step], dist[node][step] + 1)
        else:
            dist[next_node][next_step] = min(dist[next_node][next_step], dist[node][step])

if dist[t][0] == MX:
    print((-1))
else:
    print((dist[t][0]))
# Original solution
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return list(map(int, stdin.readline().split()))
def li_(): return [int(x) - 1 for x in stdin.readline().split()]
def lf(): return list(map(float, stdin.readline().split()))
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


from heapq import heappush, heappop


def dijkstra(graph: list, start, INF=float('inf')) -> list:
    n = len(graph)
    dist = [INF] * n
    visited = [False] * n

    dist[start] = 0
    que = [(0, start)]

    while que:
        cost, cur = heappop(que)
        if visited[cur]:
            continue

        visited[cur] = True

        for edgecost, nextnode in graph[cur]:
            nextcost = cost + edgecost
            if nextcost < dist[nextnode]:
                dist[nextnode] = nextcost
                heappush(que, (nextcost, nextnode))

    return dist

n, m = li()

edges = []
ins = [[] for _ in range(n)]
ots = [[] for _ in range(n)]

for _ in range(m):
    u,v = li_()
    ins[v].append(u)
    ots[u].append(v)
    edges.append((u, v))

graph = [set() for _ in range(n)]

for ui, vi in edges:
    for st in ins[ui]:
        for to in ots[vi]:
            graph[st].add((1,to))

s, t = li_()
INF = 10**8+1

dist = dijkstra(graph, s, INF)

print((-1 if dist[t] == INF else dist[t]))

# Optimized solution
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return list(map(int, stdin.readline().split()))
def li_(): return [int(x) - 1 for x in stdin.readline().split()]
def lf(): return list(map(float, stdin.readline().split()))
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


from heapq import heappush, heappop

def dijkstra(graph: list, start, INF=float('inf')) -> list:
    n = len(graph)
    dist = [INF ] *n
    visited = [False ] *n

    dist[start] = 0
    que = [(0, start)]

    while que:
        cost, cur = heappop(que)

        if visited[cur]:
            continue

        visited[cur] = True

        for nextnode, edgecost in graph[cur]:
            nextcost = cost + edgecost
            if nextcost < dist[nextnode]:
                dist[nextnode] = nextcost
                heappush(que, (nextcost, nextnode))

    return dist


n, m = li()

graph = [[] for _ in range(3*n)]
for _ in range(m):
    u, v = li_()
    for i in range(3):
        graph[i*n + u].append([(i+1)%3 * n + v, 1])

s, t = li_()
INF = 1<<62
dist = dijkstra(graph, s, INF)

print((-1 if dist[t] == INF else dist[t] // 3))
# Original solution
from queue import Queue
N, M = list(map(int, input().split()))
INF = -1
dist = [[INF]*3 for _ in range(N)]
vector = [[0]*N for _ in range(N)]

for i in range(M):
    graf1, graf2 = list(map(int, input().split()))
    vector[graf1-1][graf2-1] = 1
S, T = list(map(int, input().split()))
S, T = S-1, T-1

# BFS
dist[S][0] = 0
q = Queue(3*N+1)
q.put((S, 0))
while not q.empty():
    v, l = q.get()
    for i in range(N):
        if vector[v][i] != 1:
            continue
        new_l = (l+1)%3
        if dist[i][new_l] != INF:
            continue
        dist[i][new_l] = dist[v][l] + 1
        q.put((i, new_l))

ans = dist[T][0]
if ans == INF:
    print("-1")
else:
    print((int(ans/3)))

# Optimized solution
from queue import Queue
N, M = list(map(int, input().split()))
INF = -1
dist = [[INF]*3 for _ in range(N)]
vector = [[] for _ in range(N)]

for i in range(M):
    graf1, graf2 = list(map(int, input().split()))
    vector[graf1-1].append(graf2-1)
S, T = list(map(int, input().split()))
S, T = S-1, T-1

# BFS
dist[S][0] = 0
q = Queue(3*N+1)
q.put((S, 0))
while not q.empty():
    v, l = q.get()
    for i in vector[v]:
        new_l = (l+1)%3
        if dist[i][new_l] != INF:
            continue
        dist[i][new_l] = dist[v][l] + 1
        q.put((i, new_l))

ans = dist[T][0]
if ans == INF:
    print("-1")
else:
    print((int(ans/3)))