Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N, M = map(int, input().split())
G = {k: [] for k in range(N+1)}
for _ in range(M):
    u, v = map(int, input().split())
    G[u].append(v)
S, T = map(int, input().split())


visited = [[10**9] * (N+1) for _ in range(3)]


que = deque([(S, 0)])

while que:
    ci, t = que.popleft()
    visited[t % 3][ci] = t

    for ni in G[ci]:
        if visited[(t+1) % 3][ni] > t+1:
            que.append((ni, t+1))

if visited[0][T] == 10**9:
    print(-1)
else:
    print(visited[0][T]//3)


Fixed submission:
from collections import deque
N, M = map(int, input().split())
G = [[[] for j in range(3)] for i in range(N+1)]
for _ in range(M):
    u, v = map(int, input().split())
    for i in range(3):
        G[u][i].append((v, (i+1) % 3))
S, T = map(int, input().split())


visited = [[-1] * 3 for _ in range(N+1)]

que = deque([(S, 0, 0)])
visited[S][0] = 0

while que:
    ci, l, d = que.popleft()

    for ni, nl in G[ci][l]:
        if visited[ni][nl] == -1:
            que.append((ni, nl, d+1))
            visited[ni][nl] = d+1

print(visited[T][0]//3)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N, M = map(int, input().split())
G = {k: [] for k in range(N+1)}
for _ in range(M):
    u, v = map(int, input().split())
    G[u].append(v)
S, T = map(int, input().split())


visited = [[10**9] * (N+1) for _ in range(3)]


que = deque([(S, 0)])

while que:
    ci, t = que.popleft()
    visited[t % 3][ci] = t

    for ni in G[ci]:
        if visited[(t+1) % 3][ni] > t:
            que.append((ni, t+1))

if visited[0][T] == 10**9:
    print(-1)
else:
    print(visited[0][T]//3)


Fixed submission:
from collections import deque
N, M = map(int, input().split())
G = [[[] for j in range(3)] for i in range(N+1)]
for _ in range(M):
    u, v = map(int, input().split())
    for i in range(3):
        G[u][i].append((v, (i+1) % 3))
S, T = map(int, input().split())


visited = [[-1] * 3 for _ in range(N+1)]

que = deque([(S, 0, 0)])
visited[S][0] = 0

while que:
    ci, l, d = que.popleft()

    for ni, nl in G[ci][l]:
        if visited[ni][nl] == -1:
            que.append((ni, nl, d+1))
            visited[ni][nl] = d+1

print(visited[T][0]//3)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N, M = map(int, input().split())
G = {k: [] for k in range(N+1)}
for _ in range(M):
    u, v = map(int, input().split())
    G[u].append(v)
S, T = map(int, input().split())

visited = [[10**9] * (N+1) for _ in range(4)]


que = deque([(S, 0, 0)])

ans = -1
c = 0
while que:
    ci, t, c = que.popleft()
    if t != 0:
        visited[t][ci] = c
    if t == 3:
        t = 0
        if ci == T:
            ans = c//3
            break

    for ni in G[ci]:
        if visited[t+1][ni] > c:
            que.append((ni, t+1, c+1))

print(ans)


Fixed submission:
from collections import deque
N, M = map(int, input().split())
G = [[[] for j in range(3)] for i in range(N+1)]
for _ in range(M):
    u, v = map(int, input().split())
    for i in range(3):
        G[u][i].append((v, (i+1) % 3))
S, T = map(int, input().split())


visited = [[-1] * 3 for _ in range(N+1)]

que = deque([(S, 0, 0)])
visited[S][0] = 0

while que:
    ci, l, d = que.popleft()

    for ni, nl in G[ci][l]:
        if visited[ni][nl] == -1:
            que.append((ni, nl, d+1))
            visited[ni][nl] = d+1

print(visited[T][0]//3)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N, M = map(int, input().split())
G = {k: [] for k in range(N+1)}
for _ in range(M):
    u, v = map(int, input().split())
    G[u].append(v)
S, T = map(int, input().split())

visited = [[10**9] * (N+1) for _ in range(4)]


que = deque([(S, 0, 0)])

ans = -1
c = 0
while que:
    ci, t, c = que.popleft()
    if t != 0:
        visited[t][ci] = c
    if t == 3:
        t = 0
        if ci == T:
            ans = c//3
            break

    for ni in G[ci]:
        if visited[t+1][ni] > c:
            que.append((ni, t+1, c+1))

print(ans)


Fixed submission:
from collections import deque
N, M = map(int, input().split())
G = [[[] for j in range(3)] for i in range(N+1)]
for _ in range(M):
    u, v = map(int, input().split())
    for i in range(3):
        G[u][i].append((v, (i+1) % 3))
S, T = map(int, input().split())


visited = [[-1] * 3 for _ in range(N+1)]

que = deque([(S, 0, 0)])
visited[S][0] = 0

while que:
    ci, l, d = que.popleft()

    for ni, nl in G[ci][l]:
        if visited[ni][nl] == -1:
            que.append((ni, nl, d+1))
            visited[ni][nl] = d+1

print(visited[T][0]//3)