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

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, M = map(int, readline().split())
    G = [[] for _ in range(N)]
    for _ in range(M):
        u, v = map(int, readline().split())
        G[u - 1].append(v - 1)
    S, T = map(int, readline().split())
    S -= 1
    T -= 1

    seen = [[False] * 4 for _ in range(N)]

    def dfs(G, v, step, seen):
        if step == 0:
            if v == T:
                return 0
            else:
                step = 3

        ans = INF
        for nv in G[v]:
            if seen[nv][step - 1]:
                continue
            seen[nv][step - 1] = True
            tmp = dfs(G, nv, step - 1, seen) + 1
            seen[nv][step - 1] = False
            if ans > tmp:
                ans = tmp

        return ans

    ans = dfs(G, S, 3, seen)
    if ans == INF:
        print(-1)
    else:
        print(ans // 3)
    return


if __name__ == '__main__':
    main()


Fixed submission:
import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, M = map(int, readline().split())
    G = [[] for _ in range(N)]
    for _ in range(M):
        u, v = map(int, readline().split())
        G[u - 1].append(v - 1)
    S, T = map(int, readline().split())
    S -= 1
    T -= 1

    dist = [[-1] * 3 for _ in range(N)]
    dist[S][0] = 0
    queue = deque([(S, 0)])
    while queue:
        v, step = queue.popleft()
        if step == 0 and v == T:
            break

        for nv in G[v]:
            if dist[nv][(step - 1) % 3] != -1:
                continue
            dist[nv][(step - 1) % 3] = dist[v][step] + 1
            queue.append((nv, (step - 1) % 3))

    if dist[T][0] == -1:
        print(-1)
    else:
        print(dist[T][0] // 3)
    return


if __name__ == '__main__':
    main()

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

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, M = map(int, readline().split())
    G = [[] for _ in range(N)]
    for _ in range(M):
        u, v = map(int, readline().split())
        G[u - 1].append(v - 1)
    S, T = map(int, readline().split())
    S -= 1
    T -= 1

    seen = [[False] * 4 for _ in range(N)]

    def dfs(G, v, step, seen):
        if step == 0:
            if v == T:
                return 0
            else:
                step = 3

        ans = INF
        for nv in G[v]:
            if seen[nv][step - 1]:
                continue
            seen[nv][step - 1] = True
            tmp = dfs(G, nv, step - 1, seen) + 1
            seen[nv][step - 1] = False
            if ans > tmp:
                ans = tmp

        return ans

    ans = dfs(G, S, 3, seen)
    if ans == INF:
        print(-1)
    else:
        print(ans // 3)
    return


if __name__ == '__main__':
    main()


Fixed submission:
import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, M = map(int, readline().split())
    G = [[] for _ in range(N)]
    for _ in range(M):
        u, v = map(int, readline().split())
        G[u - 1].append(v - 1)
    S, T = map(int, readline().split())
    S -= 1
    T -= 1

    dist = [[-1] * 3 for _ in range(N)]
    dist[S][0] = 0
    queue = deque([(S, 0)])
    while queue:
        v, step = queue.popleft()
        if step == 0 and v == T:
            break

        for nv in G[v]:
            if dist[nv][(step - 1) % 3] != -1:
                continue
            dist[nv][(step - 1) % 3] = dist[v][step] + 1
            queue.append((nv, (step - 1) % 3))

    if dist[T][0] == -1:
        print(-1)
    else:
        print(dist[T][0] // 3)
    return


if __name__ == '__main__':
    main()