Buggy python submission (verdict: Time Limit Exceeded):
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix

n, m = map(int, input().split())
uv = [list(map(int, input().split())) for _ in range(m)]
s, t = map(int, input().split())

row = []
col = []
for u, v in uv:
    for i in range(3):
        uu = u + i * n
        vv = v + ((i + 1) % 3) * n
        row.append(uu)
        col.append(vv)

cost = [1] * len(row)
N = 3 * (n + 1)

g = csr_matrix((cost, (row, col)), shape=(N, N))
dist = dijkstra(g, indices=s).astype(int)
ans = dist[t]
if ans < 0:
    ans = -1
else:
    ans //= 3
print(ans)


Fixed submission:
from collections import deque

n, m = map(int, input().split())
uv = [list(map(int, input().split())) for _ in range(m)]
s, t = map(int, input().split())

adj = [[] for _ in range(3 * n)]
for u, v in uv:
    u -= 1
    v -= 1
    adj[u].append(v + n)
    adj[u + n].append(v + 2 * n)
    adj[u + 2 * n].append(v)


def bfs(s):
    q = deque([s])
    d = [-1] * (3 * n)
    d[s] = 0
    while q:
        u = q.popleft()
        for v in adj[u]:
            if d[v] == -1:
                d[v] = d[u] + 1
                q.append(v)

    return d


dist = bfs(s - 1)
ans = dist[t - 1]
print(ans // 3)

Buggy python submission (verdict: Runtime Error):
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix
import sys
sys.setrecursionlimit(10 ** 6)

n, m = map(int, input().split())
uv = [list(map(int, input().split())) for _ in range(m)]
s, t = map(int, input().split())

adj = [[] for _ in range(n)]
for u, v in uv:
    u -= 1
    v -= 1
    adj[u].append(v)

u = []
v = []
c = []


def dfs(start, now, depth):
    if depth == 3:
        u.append(start)
        v.append(now)
        c.append(1)
        return

    for nxt in adj[now]:
        dfs(start, nxt, depth + 1)


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

graph = csr_matrix((c, (u, v)), shape=(n, n))
dist = dijkstra(graph).astype(int)
ans = dist[s-1][t-1]
if ans < 0:
    ans = -1
print(ans)


Fixed submission:
from collections import deque

n, m = map(int, input().split())
uv = [list(map(int, input().split())) for _ in range(m)]
s, t = map(int, input().split())

adj = [[] for _ in range(3 * n)]
for u, v in uv:
    u -= 1
    v -= 1
    adj[u].append(v + n)
    adj[u + n].append(v + 2 * n)
    adj[u + 2 * n].append(v)


def bfs(s):
    q = deque([s])
    d = [-1] * (3 * n)
    d[s] = 0
    while q:
        u = q.popleft()
        for v in adj[u]:
            if d[v] == -1:
                d[v] = d[u] + 1
                q.append(v)

    return d


dist = bfs(s - 1)
ans = dist[t - 1]
print(ans // 3)

Buggy python submission (verdict: Runtime Error):
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix

n, m = map(int, input().split())
uv = [list(map(int, input().split())) for _ in range(m)]
s, t = map(int, input().split())

adj = [[] for _ in range(n)]
for u, v in uv:
    u -= 1
    v -= 1
    adj[u].append(v)

u = []
v = []
c = []


def dfs(start, now, depth):
    if depth == 3:
        u.append(start)
        v.append(now)
        c.append(1)
        return

    for nxt in adj[now]:
        dfs(start, nxt, depth + 1)


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

graph = csr_matrix((c, (u, v)), shape=(n, n))
dist = dijkstra(graph).astype(int)
ans = dist[s-1][t-1]
if ans < 0:
    ans = -1
print(ans)


Fixed submission:
from collections import deque

n, m = map(int, input().split())
uv = [list(map(int, input().split())) for _ in range(m)]
s, t = map(int, input().split())

adj = [[] for _ in range(3 * n)]
for u, v in uv:
    u -= 1
    v -= 1
    adj[u].append(v + n)
    adj[u + n].append(v + 2 * n)
    adj[u + 2 * n].append(v)


def bfs(s):
    q = deque([s])
    d = [-1] * (3 * n)
    d[s] = 0
    while q:
        u = q.popleft()
        for v in adj[u]:
            if d[v] == -1:
                d[v] = d[u] + 1
                q.append(v)

    return d


dist = bfs(s - 1)
ans = dist[t - 1]
print(ans // 3)