Buggy python submission (verdict: Time Limit Exceeded):
import sys
from heapq import heappop, heappush, heapify
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**7)
INF = float("inf")

n, m = map(int, input().split())
g = [[] for _ in range(n)]
for _ in range(m):
    u, v = map(lambda x: int(x)-1, input().split())
    g[u].append(v)
s, t = map(lambda x: int(x)-1, input().split())

g2 = [set() for _ in range(n)]
visited = [False] * n

def dfs(v):
    if visited[v]: return
    visited[v] = True
    for nv1 in g[v]:
        for nv2 in g[nv1]:
            for nv3 in g[nv2]:
                if nv3 not in g2[v]:
                    g2[v].add(nv3)
                    dfs(nv3)

for i in range(n): dfs(i)
for i in range(n):
    g2[i] = list(map(lambda x: (x, 1), list(g2[i])))

def dijkstra(graph, n, s):
    d = [INF] * n
    d[s], q = 0, []
    heappush(q, (0, s))
    while q:
        dist, v = heappop(q)
        if d[v] < dist: continue
        for nv, cost in graph[v]:
            if d[nv] > d[v] + cost:
                d[nv] = d[v] + cost
                heappush(q, (d[nv], nv))
    return d

ans = dijkstra(g2, n, s)[t]
print(-1 if ans == INF else ans)


Fixed submission:
from heapq import heappop, heappush, heapify

INF = float("inf")
n, m = map(int, input().split())
idx = [[i*3 + j for j in range(3)] for i in range(n)]
g = [[] for _ in range(n*3)]
for _ in range(m):
    u, v = map(lambda x: int(x)-1, input().split())
    for i in range(3):
        g[idx[u][i]].append((idx[v][(i+1) % 3], 1))
s, t = map(lambda x: int(x)-1, input().split())

def dijkstra(graph, n, s):
    d = [INF] * n
    d[s], q = 0, []
    heappush(q, (0, s))
    while q:
        dist, v = heappop(q)
        if d[v] < dist: continue
        for nv, cost in graph[v]:
            if d[nv] > d[v] + cost:
                d[nv] = d[v] + cost
                heappush(q, (d[nv], nv))
    return d

res = dijkstra(g, n*3, idx[s][0])[idx[t][0]]
print(-1 if res == INF else res // 3)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from heapq import heappop, heappush, heapify
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**7)
INF = float("inf")

n, m = map(int, input().split())
g = [[] for _ in range(n)]
for _ in range(m):
    u, v = map(lambda x: int(x)-1, input().split())
    g[u].append(v)
s, t = map(lambda x: int(x)-1, input().split())

g2 = [[] for _ in range(n)]
visited = [False] * n

def dfs(v):
    if visited[v]: return
    visited[v] = True
    for nv1 in g[v]:
        for nv2 in g[nv1]:
            for nv3 in g[nv2]:
                g2[v].append((nv3, 1))
                dfs(nv3)

for i in range(n): dfs(i)

def dijkstra(graph, n, s):
    d = [INF] * n
    d[s], q = 0, []
    heappush(q, (0, s))
    while q:
        dist, v = heappop(q)
        if d[v] < dist: continue
        for nv, cost in graph[v]:
            if d[nv] > d[v] + cost:
                d[nv] = d[v] + cost
                heappush(q, (d[nv], nv))
    return d

ans = dijkstra(g2, n, s)[t]
print(-1 if ans == INF else ans)


Fixed submission:
from heapq import heappop, heappush, heapify

INF = float("inf")
n, m = map(int, input().split())
idx = [[i*3 + j for j in range(3)] for i in range(n)]
g = [[] for _ in range(n*3)]
for _ in range(m):
    u, v = map(lambda x: int(x)-1, input().split())
    for i in range(3):
        g[idx[u][i]].append((idx[v][(i+1) % 3], 1))
s, t = map(lambda x: int(x)-1, input().split())

def dijkstra(graph, n, s):
    d = [INF] * n
    d[s], q = 0, []
    heappush(q, (0, s))
    while q:
        dist, v = heappop(q)
        if d[v] < dist: continue
        for nv, cost in graph[v]:
            if d[nv] > d[v] + cost:
                d[nv] = d[v] + cost
                heappush(q, (d[nv], nv))
    return d

res = dijkstra(g, n*3, idx[s][0])[idx[t][0]]
print(-1 if res == INF else res // 3)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from heapq import heappop, heappush, heapify
sys.setrecursionlimit(10**7)
INF = float("inf")

n, m = map(int, input().split())
g = [[] for _ in range(n)]
for _ in range(m):
    u, v = map(lambda x: int(x)-1, input().split())
    g[u].append(v)
s, t = map(lambda x: int(x)-1, input().split())

g2 = [[] for _ in range(n)]
visited = [False] * n

def dfs(v):
    if visited[v]: return
    visited[v] = True
    for nv1 in g[v]:
        for nv2 in g[nv1]:
            for nv3 in g[nv2]:
                g2[v].append((nv3, 1))
                dfs(nv3)

for i in range(n): dfs(i)

def dijkstra(graph, n, s):
    d = [INF] * n
    d[s], q = 0, []
    heappush(q, (0, s))
    while q:
        dist, v = heappop(q)
        if d[v] < dist: continue
        for nv, cost in graph[v]:
            if d[nv] > d[v] + cost:
                d[nv] = d[v] + cost
                heappush(q, (d[nv], nv))
    return d

ans = dijkstra(g2, n, s)[t]
print(-1 if ans == INF else ans)


Fixed submission:
from heapq import heappop, heappush, heapify

INF = float("inf")
n, m = map(int, input().split())
idx = [[i*3 + j for j in range(3)] for i in range(n)]
g = [[] for _ in range(n*3)]
for _ in range(m):
    u, v = map(lambda x: int(x)-1, input().split())
    for i in range(3):
        g[idx[u][i]].append((idx[v][(i+1) % 3], 1))
s, t = map(lambda x: int(x)-1, input().split())

def dijkstra(graph, n, s):
    d = [INF] * n
    d[s], q = 0, []
    heappush(q, (0, s))
    while q:
        dist, v = heappop(q)
        if d[v] < dist: continue
        for nv, cost in graph[v]:
            if d[nv] > d[v] + cost:
                d[nv] = d[v] + cost
                heappush(q, (d[nv], nv))
    return d

res = dijkstra(g, n*3, idx[s][0])[idx[t][0]]
print(-1 if res == INF else res // 3)