Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 6)


data = sys.stdin.readlines()
N, M = map(int, data[0].split())
edge = [[[] for _ in range(N)] for _ in range(3)]
for i in range(1, M + 1):
    u, v = map(int, data[i].split())
    u -= 1
    v -= 1
    edge[0][u].append(v)
    edge[1][u].append(v)
    edge[2][u].append(v)
S, T = map(lambda x: int(x) - 1, data[M + 1].split())


def dfs(n, step):
    global T, ans
    rst = False
    if n == T and step % 3 == 0:
        rst |= True
    else:
        d = step % 3
        for e in edge[d][n]:
            if status[(d + 1) % 3][e] > step + 1:
                status[(d + 1) % 3][e] = step + 1
                rst |= dfs(e, step + 1)
    return rst


INF = float('inf')
status = [[INF] * N for _ in range(3)]
ans = 10 ** 10
if dfs(S, 0):
    print(status[0][T] // 3)
else:
    print(-1)


Fixed submission:
import sys
from collections import deque


data = sys.stdin.readlines()
N, M = map(int, data[0].split())
edge = [[] for _ in range(N)]
for i in range(1, M + 1):
    u, v = map(int, data[i].split())
    u -= 1
    v -= 1
    edge[u].append(v)
S, T = map(lambda x: int(x) - 1, data[M + 1].split())

visited = set()
q = deque()
q.append((0, S))
ans = 10 ** 10
while len(q) > 0:
    c, v = q.popleft()
    key = (c % 3, v)
    if key in visited:
        continue
    visited.add(key)
    if v == T and c % 3 == 0:
        ans = min(ans, c // 3)
    else:
        for e in edge[v]:
            newKey = (c + 1, e)
            q.append(newKey)

if ans == 10 ** 10:
    print(-1)
else:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque


data = sys.stdin.readlines()
N, M = map(int, data[0].split())
edge = [[] for _ in range(N)]
for i in range(1, M + 1):
    u, v = map(int, data[i].split())
    u -= 1
    v -= 1
    edge[u].append(v)
S, T = map(lambda x: int(x) - 1, data[M + 1].split())


def bfs1(s):
    q = deque()
    q.append((s, 0))

    while len(q) > 0:
        v, step = q.popleft()
        if step == 3:
            edge3[s].append(v)
        else:
            for nv in edge[v]:
                q.append((nv, step + 1))


def bfs2(n):
    global T
    q = deque()
    status[n] = 0
    q.append(n)

    while len(q) > 0:
        v = q.popleft()
        for nv in edge3[v]:
            if status[nv] == -1:
                status[nv] = status[v] + 1
                q.append(nv)


edge3 = [[] for _ in range(N)]
for i in range(N):
    bfs1(i)


status = [-1] * N
status[S] = 0
bfs2(S)
print(status[T])


Fixed submission:
import sys
from collections import deque


data = sys.stdin.readlines()
N, M = map(int, data[0].split())
edge = [[] for _ in range(N)]
for i in range(1, M + 1):
    u, v = map(int, data[i].split())
    u -= 1
    v -= 1
    edge[u].append(v)
S, T = map(lambda x: int(x) - 1, data[M + 1].split())

visited = set()
q = deque()
q.append((0, S))
ans = 10 ** 10
while len(q) > 0:
    c, v = q.popleft()
    key = (c % 3, v)
    if key in visited:
        continue
    visited.add(key)
    if v == T and c % 3 == 0:
        ans = min(ans, c // 3)
    else:
        for e in edge[v]:
            newKey = (c + 1, e)
            q.append(newKey)

if ans == 10 ** 10:
    print(-1)
else:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
sys.setrecursionlimit(10 ** 6)


N, M = map(int, input().split())
edge = [[] for _ in range(N)]
for _ in range(M):
    u, v = map(lambda x: int(x) - 1, input().split())
    edge[u].append(v)
S, T = map(lambda x: int(x) - 1, input().split())


def bfs1(s):
    q = deque()
    q.append((s, 0))

    while len(q) > 0:
        v, step = q.popleft()
        if step == 3:
            edge3[s].add(v)
        else:
            for nv in edge[v]:
                q.append((nv, step + 1))


def bfs2(n):
    global T
    q = deque()
    status[n] = 0
    q.append(n)

    while len(q) > 0:
        v = q.popleft()
        for nv in edge3[v]:
            if status[nv] == -1:
                status[nv] = status[v] + 1
                q.append(nv)


edge3 = [set() for _ in range(N)]
for i in range(N):
    bfs1(i)


status = [-1] * N
status[S] = 0
bfs2(S)
print(status[T])


Fixed submission:
import sys
from collections import deque


data = sys.stdin.readlines()
N, M = map(int, data[0].split())
edge = [[] for _ in range(N)]
for i in range(1, M + 1):
    u, v = map(int, data[i].split())
    u -= 1
    v -= 1
    edge[u].append(v)
S, T = map(lambda x: int(x) - 1, data[M + 1].split())

visited = set()
q = deque()
q.append((0, S))
ans = 10 ** 10
while len(q) > 0:
    c, v = q.popleft()
    key = (c % 3, v)
    if key in visited:
        continue
    visited.add(key)
    if v == T and c % 3 == 0:
        ans = min(ans, c // 3)
    else:
        for e in edge[v]:
            newKey = (c + 1, e)
            q.append(newKey)

if ans == 10 ** 10:
    print(-1)
else:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
sys.setrecursionlimit(10 ** 6)


N, M = map(int, input().split())
edge = [[] for _ in range(N)]
for _ in range(M):
    u, v = map(lambda x: int(x) - 1, input().split())
    edge[u].append(v)
S, T = map(lambda x: int(x) - 1, input().split())


def dfs1(s, n, step):
    if step == 3:
        edge3[s].add(n)
    else:
        for e in edge[n]:
            dfs1(s, e, step + 1)


def bfs2(n):
    global T
    q = deque()
    status[n] = 0
    q.append(n)

    while len(q) > 0:
        v = q.popleft()
        for nv in edge3[v]:
            if status[nv] == -1:
                status[nv] = status[v] + 1
                q.append(nv)


edge3 = [set() for _ in range(N)]
for i in range(N):
    dfs1(i, i, 0)


status = [-1] * N
status[S] = 0
bfs2(S)
print(status[T])


Fixed submission:
import sys
from collections import deque


data = sys.stdin.readlines()
N, M = map(int, data[0].split())
edge = [[] for _ in range(N)]
for i in range(1, M + 1):
    u, v = map(int, data[i].split())
    u -= 1
    v -= 1
    edge[u].append(v)
S, T = map(lambda x: int(x) - 1, data[M + 1].split())

visited = set()
q = deque()
q.append((0, S))
ans = 10 ** 10
while len(q) > 0:
    c, v = q.popleft()
    key = (c % 3, v)
    if key in visited:
        continue
    visited.add(key)
    if v == T and c % 3 == 0:
        ans = min(ans, c // 3)
    else:
        for e in edge[v]:
            newKey = (c + 1, e)
            q.append(newKey)

if ans == 10 ** 10:
    print(-1)
else:
    print(ans)