Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())


N,M = inpl()
lines1 = defaultdict(set)

for _ in range(M):
    u,v = inpl()
    u -= 1
    v -= 1
    lines1[u].add(v)

S,T = inpl()
S -= 1
T -= 1

#MEMO = [[set() for i in range(3)] for _ in range(N)]
#visited = [False]*N

lines2 = defaultdict(set)
def dfs(start,s,n): #s: 今いる , n: 何歩目
    if n == 3:
        lines2[start].add(s)
    else:
        for t in lines1[s]:
            dfs(start,t,n+1)

for i in range(N):
    dfs(i,i,0)

'''
print()
for s in range(N):
    for t in lines2[s]:
        print(s,t)
'''

def search (s,w_0): #s->t
    global weight
    global q

    for t in list(lines2[s]):
        w = w_0 + 1
        if weight[t] > w:
            heapq.heappush(q, [w,t])
            weight[t] = w

weight = [INF]*N
weight[S] = 0
q = [[0,S]]
heapq.heapify(q)
while q:
    w,n = heapq.heappop(q)
    search(n,w)

if weight[T] == INF:
    print(-1)
else:
    print(weight[T])


Fixed submission:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())


N,M = inpl()
lines = defaultdict(set)

for _ in range(M):
    u,v = inpl()
    u -= 1
    v -= 1
    lines[u].add(v+N)
    lines[u+N].add(v+2*N)
    lines[u+2*N].add(v)

S,T = inpl()
S -= 1
T -= 1

def search (s,w_0): #s->t
	global weight
	global q

	for t in list(lines[s]):
		w = w_0 + 1
		if weight[t] > w:
			heapq.heappush(q, [w,t])
			weight[t] = w

# Start, S
weight = [INF]*(N*3)
weight[S] = 0
q = [[0,S]]
heapq.heapify(q)
while q:
	w,n = heapq.heappop(q)
	search(n,w)

ans = weight[T]

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

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())


N,M = inpl()
lines1 = defaultdict(set)

for _ in range(M):
    u,v = inpl()
    u -= 1
    v -= 1
    lines1[u].add(v)

S,T = inpl()
S -= 1
T -= 1

MEMO = [set() for _ in range(N)]
visited = [[False]*2 for _ in range(N)]

lines2 = defaultdict(set)
def dfs(start,s,n): #s: 今いる , n: 何歩目
    if n == 2:
        lines2[start] = copy.deepcopy(lines1[s])
    elif n == 1:
        if visited[s][1]:
            lines2[start].add(MEMO[s])
        else:
            for t in lines1[s]:
                MEMO[s].add(t)
                dfs(start,t,n+1)
    elif n == 0:
        for t in lines1[s]:
            dfs(start,t,n+1)


for i in range(N):
    dfs(i,i,0)


def search (s,w_0): #s->t
    global weight
    global q

    for t in list(lines2[s]):
        w = w_0 + 1
        if weight[t] > w:
            heapq.heappush(q, [w,t])
            weight[t] = w

weight = [INF]*N
weight[S] = 0
q = [[0,S]]
heapq.heapify(q)
while q:
    w,n = heapq.heappop(q)
    search(n,w)

if weight[T] == INF:
    print(-1)
else:
    print(weight[T])


Fixed submission:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())


N,M = inpl()
lines = defaultdict(set)

for _ in range(M):
    u,v = inpl()
    u -= 1
    v -= 1
    lines[u].add(v+N)
    lines[u+N].add(v+2*N)
    lines[u+2*N].add(v)

S,T = inpl()
S -= 1
T -= 1

def search (s,w_0): #s->t
	global weight
	global q

	for t in list(lines[s]):
		w = w_0 + 1
		if weight[t] > w:
			heapq.heappush(q, [w,t])
			weight[t] = w

# Start, S
weight = [INF]*(N*3)
weight[S] = 0
q = [[0,S]]
heapq.heapify(q)
while q:
	w,n = heapq.heappop(q)
	search(n,w)

ans = weight[T]

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

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())


N,M = inpl()
lines1 = defaultdict(set)

for _ in range(M):
    u,v = inpl()
    u -= 1
    v -= 1
    lines1[u].add(v)

S,T = inpl()
S -= 1
T -= 1

MEMO = [set() for _ in range(N)]
visited = [[False]*2 for _ in range(N)]

lines2 = defaultdict(set)
def dfs(start,s,n): #s: 今いる , n: 何歩目
    if n == 2:
        lines2[start] = copy.deepcopy(lines1[s])
    elif n == 1:
        if visited[s][1]:
            lines2[start].add(MEMO[s])
        else:
            for t in lines1[s]:
                MEMO[s].add(t)
                dfs(start,t,n+1)
    elif n == 0:
        for t in lines1[s]:
            dfs(start,t,n+1)


for i in range(N):
    dfs(i,i,0)


def search (s,w_0): #s->t
    global weight
    global q

    for t in list(lines2[s]):
        w = w_0 + 1
        if weight[t] > w:
            heapq.heappush(q, [w,t])
            weight[t] = w

weight = [INF]*N
weight[S] = 0
q = [[0,S]]
heapq.heapify(q)
while q:
    w,n = heapq.heappop(q)
    search(n,w)

if weight[T] == INF:
    print(-1)
else:
    print(weight[T])


Fixed submission:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())


N,M = inpl()
lines = defaultdict(set)

for _ in range(M):
    u,v = inpl()
    u -= 1
    v -= 1
    lines[u].add(v+N)
    lines[u+N].add(v+2*N)
    lines[u+2*N].add(v)

S,T = inpl()
S -= 1
T -= 1

def search (s,w_0): #s->t
	global weight
	global q

	for t in list(lines[s]):
		w = w_0 + 1
		if weight[t] > w:
			heapq.heappush(q, [w,t])
			weight[t] = w

# Start, S
weight = [INF]*(N*3)
weight[S] = 0
q = [[0,S]]
heapq.heapify(q)
while q:
	w,n = heapq.heappop(q)
	search(n,w)

ans = weight[T]

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

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())


N,M = inpl()
lines1 = defaultdict(set)

for _ in range(M):
    u,v = inpl()
    u -= 1
    v -= 1
    lines1[u].add(v)

S,T = inpl()
S -= 1
T -= 1

#MEMO = [[set() for i in range(3)] for _ in range(N)]
#visited = [False]*N

lines2 = defaultdict(set)
def dfs(start,s,n): #s: 今いる , n: 何歩目
    if n == 3:
        lines2[start].add(s)
    else:
        for t in lines1[s]:
            dfs(start,t,n+1)

for i in range(N):
    dfs(i,i,0)

'''
print()
for s in range(N):
    for t in lines2[s]:
        print(s,t)
'''

def search (s,w_0): #s->t
    global weight
    global q

    for t in list(lines2[s]):
        w = w_0 + 1
        if weight[t] > w:
            heapq.heappush(q, [w,t])
            weight[t] = w

weight = [INF]*N
weight[S] = 0
q = [[0,S]]
heapq.heapify(q)
while q:
    w,n = heapq.heappop(q)
    search(n,w)

if weight[T] == INF:
    print(-1)
else:
    print(weight[T])


Fixed submission:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())


N,M = inpl()
lines = defaultdict(set)

for _ in range(M):
    u,v = inpl()
    u -= 1
    v -= 1
    lines[u].add(v+N)
    lines[u+N].add(v+2*N)
    lines[u+2*N].add(v)

S,T = inpl()
S -= 1
T -= 1

def search (s,w_0): #s->t
	global weight
	global q

	for t in list(lines[s]):
		w = w_0 + 1
		if weight[t] > w:
			heapq.heappush(q, [w,t])
			weight[t] = w

# Start, S
weight = [INF]*(N*3)
weight[S] = 0
q = [[0,S]]
heapq.heapify(q)
while q:
	w,n = heapq.heappop(q)
	search(n,w)

ans = weight[T]

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