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

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N, M = map(int, input().split())
        edges = [tuple(map(int, input().split())) for _ in range(M)]
        S, T = map(int, input().split())

        edge_map = collections.defaultdict(list)
        for u, v in edges:
            edge_map[u - 1].append(v - 1)

        q = queue.Queue()
        q.put((S - 1, 0))

        INF = 10 ** 5
        dist = [[INF] * 3 for _ in range(N)]
        dist[S - 1][0] = 0
        while not q.empty():
            node, kkp = q.get()

            for next_node in edge_map[node]:
                next_kkp = (kkp + 1) % 3
                if dist[next_node][next_kkp] == INF:
                    dist[next_node][next_kkp] = dist[node][kkp] + 1
                    q.put((next_node, next_kkp))

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


# Run main
if __name__ == '__main__':
    AtCoder().main()


Fixed submission:
import collections
import queue
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N, M = map(int, input().split())
        edges = [tuple(map(int, input().split())) for _ in range(M)]
        S, T = map(int, input().split())

        edge_map = collections.defaultdict(list)
        for u, v in edges:
            edge_map[u - 1].append(v - 1)

        q = queue.Queue()
        q.put((S - 1, 0))

        INF = 10 ** 5
        dist = [[INF] * 3 for _ in range(N)]
        dist[S - 1][0] = 0
        while not q.empty():
            node, kkp = q.get()

            for next_node in edge_map[node]:
                next_kkp = (kkp + 1) % 3
                if dist[next_node][next_kkp] == INF:
                    dist[next_node][next_kkp] = dist[node][kkp] + 1
                    q.put((next_node, next_kkp))

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


# Run main
if __name__ == '__main__':
    AtCoder().main()

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

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N, M = map(int, input().split())
        edges = [tuple(map(int, input().split())) for _ in range(M)]
        S, T = map(int, input().split())

        edge_map = collections.defaultdict(list)
        for u, v in edges:
            edge_map[u].append(v)

        q = queue.Queue()
        q.put((S, 0))

        dist = collections.defaultdict(lambda: [10 ** 5] * 3)
        dist[S][0] = 0
        while not q.empty():
            node, total_path_length = q.get()

            for next_node in edge_map[node]:
                nl = (total_path_length + 1) % 3
                if dist[next_node][nl] == 10 ** 5:
                    dist[next_node][nl] = total_path_length + 1
                    q.put((next_node, total_path_length + 1))

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


# Run main
if __name__ == '__main__':
    AtCoder().main()


Fixed submission:
import collections
import queue
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N, M = map(int, input().split())
        edges = [tuple(map(int, input().split())) for _ in range(M)]
        S, T = map(int, input().split())

        edge_map = collections.defaultdict(list)
        for u, v in edges:
            edge_map[u - 1].append(v - 1)

        q = queue.Queue()
        q.put((S - 1, 0))

        INF = 10 ** 5
        dist = [[INF] * 3 for _ in range(N)]
        dist[S - 1][0] = 0
        while not q.empty():
            node, kkp = q.get()

            for next_node in edge_map[node]:
                next_kkp = (kkp + 1) % 3
                if dist[next_node][next_kkp] == INF:
                    dist[next_node][next_kkp] = dist[node][kkp] + 1
                    q.put((next_node, next_kkp))

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


# Run main
if __name__ == '__main__':
    AtCoder().main()

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

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N, M = map(int, input().split())
        edges = [tuple(map(int, input().split())) for _ in range(M)]
        S, T = map(int, input().split())

        edge_map = collections.defaultdict(list)
        for u, v in edges:
            edge_map[u].append(v)

        q = queue.Queue()
        q.put((S, 0, 1))

        dp = collections.defaultdict(lambda: 10 ** 5)
        ans = 10 ** 5
        while not q.empty():
            node, kkp, cnt = q.get()

            kkp += 1

            if kkp < 3:
                for next_node in edge_map[node]:
                    q.put((next_node, kkp, cnt))
            else:
                for next_node in edge_map[node]:
                    if next_node == T:
                        ans = min(ans, cnt)
                        q = queue.Queue()
                        break
                    elif dp[next_node] > cnt + 1:
                        dp[next_node] = cnt + 1
                        q.put((next_node, 0, cnt + 1))

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

# Run main
if __name__ == '__main__':
    AtCoder().main()

Fixed submission:
import collections
import queue
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N, M = map(int, input().split())
        edges = [tuple(map(int, input().split())) for _ in range(M)]
        S, T = map(int, input().split())

        edge_map = collections.defaultdict(list)
        for u, v in edges:
            edge_map[u - 1].append(v - 1)

        q = queue.Queue()
        q.put((S - 1, 0))

        INF = 10 ** 5
        dist = [[INF] * 3 for _ in range(N)]
        dist[S - 1][0] = 0
        while not q.empty():
            node, kkp = q.get()

            for next_node in edge_map[node]:
                next_kkp = (kkp + 1) % 3
                if dist[next_node][next_kkp] == INF:
                    dist[next_node][next_kkp] = dist[node][kkp] + 1
                    q.put((next_node, next_kkp))

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


# Run main
if __name__ == '__main__':
    AtCoder().main()

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

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N, M = map(int, input().split())
        edges = [tuple(map(int, input().split())) for _ in range(M)]
        S, T = map(int, input().split())

        edge_map = collections.defaultdict(list)
        for u, v in edges:
            edge_map[u].append(v)

        q = queue.Queue()
        q.put((S, 0, 1))

        dp = collections.defaultdict(lambda: 10 ** 5)
        ans = 10 ** 5
        while not q.empty():
            node, kkp, cnt = q.get()

            kkp += 1

            if kkp < 3:
                for next_node in edge_map[node]:
                    q.put((next_node, kkp, cnt))
            else:
                for next_node in edge_map[node]:
                    if next_node == T:
                        ans = min(ans, cnt)
                        q = queue.Queue()
                        break
                    elif dp[next_node] > cnt + 1:
                        dp[next_node] = cnt + 1
                        q.put((next_node, 0, cnt + 1))

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

# Run main
if __name__ == '__main__':
    AtCoder().main()

Fixed submission:
import collections
import queue
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N, M = map(int, input().split())
        edges = [tuple(map(int, input().split())) for _ in range(M)]
        S, T = map(int, input().split())

        edge_map = collections.defaultdict(list)
        for u, v in edges:
            edge_map[u - 1].append(v - 1)

        q = queue.Queue()
        q.put((S - 1, 0))

        INF = 10 ** 5
        dist = [[INF] * 3 for _ in range(N)]
        dist[S - 1][0] = 0
        while not q.empty():
            node, kkp = q.get()

            for next_node in edge_map[node]:
                next_kkp = (kkp + 1) % 3
                if dist[next_node][next_kkp] == INF:
                    dist[next_node][next_kkp] = dist[node][kkp] + 1
                    q.put((next_node, next_kkp))

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


# Run main
if __name__ == '__main__':
    AtCoder().main()