Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import sys
input = sys.stdin.readline


def main():
    node_num, edge_num = map(int, input().split())
    graph_data = defaultdict(set)

    abcde = [list(map(int, input().split())) for i in range(edge_num)]

    for i in range(edge_num):
        a, b = abcde[i]
        graph_data[a].add(b)

    start, goal = map(int, input().split())
    visit_flg = [0] * (node_num + 1)
    visit_flg[start] = 1

    now_set = set()
    now_set.add(start)
    next_set = set()

    ans = 0

    while len(now_set) > 0:
        ans += 1

        if visit_flg[goal]:
            break

        for now_node in now_set:
            search_now_set = set()
            search_now_set.add(now_node)
            search_next_set = set()

            for i in range(3):
                for search_now_node in search_now_set:
                    search_next_set |= graph_data[search_now_node]
                search_now_set = search_next_set
                search_next_set = set()

            for ele_check in search_now_set:
                if visit_flg[ele_check]:
                    continue
                else:
                    visit_flg[ele_check] = 1
                    next_set.add(ele_check)

                if visit_flg[goal]:
                    break

        if visit_flg[goal]:
            break

        now_set = next_set
        next_set = set()

    if visit_flg[goal]:
        print(ans)
    else:
        print(-1)












if __name__ == '__main__':
    main()

Fixed submission:
from collections import defaultdict
import sys
input = sys.stdin.readline


def main():
    node_num, edge_num = map(int, input().split())
    graph_data = defaultdict(set)

    abcde = [list(map(int, input().split())) for i in range(edge_num)]

    for i in range(edge_num):
        a, b = abcde[i]
        graph_data[a].add(b)

    start, goal = map(int, input().split())
    visit_flg = [0] * (node_num + 1)
    visit_flg2 = [0] * (node_num + 1)
    visit_flg3 = [0] * (node_num + 1)
    visit_flg[start] = 1

    now_set = set()
    now_set.add(start)
    next_set = set()

    ans = 0

    while len(now_set) > 0:
        ans += 1

        if visit_flg[goal]:
            break

        for now_node in now_set:
            search_now_set = set()
            search_now_set.add(now_node)
            search_next_set = set()

            for i in range(3):
                for search_now_node in search_now_set:
                    if i == 1 and visit_flg2[search_now_node]:
                        continue
                    elif i == 1:
                        visit_flg2[search_now_node] = 1
                    if i == 2 and visit_flg3[search_now_node]:
                        continue
                    elif i == 2:
                        visit_flg3[search_now_node] = 1
                    search_next_set |= graph_data[search_now_node]
                search_now_set = search_next_set
                search_next_set = set()

            for ele_check in search_now_set:
                if visit_flg[ele_check]:
                    continue
                else:
                    visit_flg[ele_check] = 1
                    next_set.add(ele_check)

                if visit_flg[goal]:
                    break

        if visit_flg[goal]:
            break

        now_set = next_set
        next_set = set()

    if visit_flg[goal]:
        print(ans)
    else:
        print(-1)












if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import sys
input = sys.stdin.readline


def main():
    node_num, edge_num = map(int, input().split())
    graph_data = defaultdict(set)

    abcde = [list(map(int, input().split())) for i in range(edge_num)]

    for i in range(edge_num):
        a, b = abcde[i]
        graph_data[a].add(b)

    start, goal = map(int, input().split())
    visit_flg = [0 for i in range(node_num + 1)]
    visit_flg[start] += 1

    now_set = set()
    now_set.add(start)
    next_set = set()

    ans = 0

    while len(now_set) > 0:
        ans += 1

        for now_node in now_set:
            search_now_set = set()
            search_now_set.add(now_node)
            search_next_set = set()

            for i in range(3):
                for search_now_node in search_now_set:
                    search_next_set |= graph_data[search_now_node]
                search_now_set = search_next_set
                search_next_set = set()

            for ele_check in search_now_set:
                if visit_flg[ele_check]:
                    continue
                else:
                    visit_flg[ele_check] = 1
                    next_set.add(ele_check)

        if visit_flg[goal]:
            break

        now_set = next_set
        next_set = set()

    if visit_flg[goal]:
        print(ans)
    else:
        print(-1)












if __name__ == '__main__':
    main()

Fixed submission:
from collections import defaultdict
import sys
input = sys.stdin.readline


def main():
    node_num, edge_num = map(int, input().split())
    graph_data = defaultdict(set)

    abcde = [list(map(int, input().split())) for i in range(edge_num)]

    for i in range(edge_num):
        a, b = abcde[i]
        graph_data[a].add(b)

    start, goal = map(int, input().split())
    visit_flg = [0] * (node_num + 1)
    visit_flg2 = [0] * (node_num + 1)
    visit_flg3 = [0] * (node_num + 1)
    visit_flg[start] = 1

    now_set = set()
    now_set.add(start)
    next_set = set()

    ans = 0

    while len(now_set) > 0:
        ans += 1

        if visit_flg[goal]:
            break

        for now_node in now_set:
            search_now_set = set()
            search_now_set.add(now_node)
            search_next_set = set()

            for i in range(3):
                for search_now_node in search_now_set:
                    if i == 1 and visit_flg2[search_now_node]:
                        continue
                    elif i == 1:
                        visit_flg2[search_now_node] = 1
                    if i == 2 and visit_flg3[search_now_node]:
                        continue
                    elif i == 2:
                        visit_flg3[search_now_node] = 1
                    search_next_set |= graph_data[search_now_node]
                search_now_set = search_next_set
                search_next_set = set()

            for ele_check in search_now_set:
                if visit_flg[ele_check]:
                    continue
                else:
                    visit_flg[ele_check] = 1
                    next_set.add(ele_check)

                if visit_flg[goal]:
                    break

        if visit_flg[goal]:
            break

        now_set = next_set
        next_set = set()

    if visit_flg[goal]:
        print(ans)
    else:
        print(-1)












if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import math

import sys
input = sys.stdin.readline

from math import factorial
# 組み合わせ
def combinations_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))


def main():
    node_num, edge_num = map(int, input().split())
    graph_data = defaultdict(set)

    for i in range(edge_num):
        a, b = map(int, input().split())
        graph_data[a].add(b)

    start, goal = map(int, input().split())
    visit_flg = [0 for i in range(node_num + 1)]
    visit_flg[start] += 1

    now_set = set()
    now_set.add(start)
    next_set = set()

    ans = 0

    while len(now_set) > 0:
        ans += 1

        for now_node in now_set:
            search_now_set = set()
            search_now_set.add(now_node)
            search_next_set = set()

            for i in range(3):
                for search_now_node in search_now_set:
                    search_next_set |= graph_data[search_now_node]
                search_now_set = search_next_set
                search_next_set = set()

            for ele_check in search_now_set:
                if visit_flg[ele_check]:
                    continue
                else:
                    visit_flg[ele_check] = 1
                    next_set.add(ele_check)

        if visit_flg[goal]:
            break

        now_set = next_set
        next_set = set()

    if visit_flg[goal]:
        print(ans)
    else:
        print(-1)












if __name__ == '__main__':
    main()

Fixed submission:
from collections import defaultdict
import sys
input = sys.stdin.readline


def main():
    node_num, edge_num = map(int, input().split())
    graph_data = defaultdict(set)

    abcde = [list(map(int, input().split())) for i in range(edge_num)]

    for i in range(edge_num):
        a, b = abcde[i]
        graph_data[a].add(b)

    start, goal = map(int, input().split())
    visit_flg = [0] * (node_num + 1)
    visit_flg2 = [0] * (node_num + 1)
    visit_flg3 = [0] * (node_num + 1)
    visit_flg[start] = 1

    now_set = set()
    now_set.add(start)
    next_set = set()

    ans = 0

    while len(now_set) > 0:
        ans += 1

        if visit_flg[goal]:
            break

        for now_node in now_set:
            search_now_set = set()
            search_now_set.add(now_node)
            search_next_set = set()

            for i in range(3):
                for search_now_node in search_now_set:
                    if i == 1 and visit_flg2[search_now_node]:
                        continue
                    elif i == 1:
                        visit_flg2[search_now_node] = 1
                    if i == 2 and visit_flg3[search_now_node]:
                        continue
                    elif i == 2:
                        visit_flg3[search_now_node] = 1
                    search_next_set |= graph_data[search_now_node]
                search_now_set = search_next_set
                search_next_set = set()

            for ele_check in search_now_set:
                if visit_flg[ele_check]:
                    continue
                else:
                    visit_flg[ele_check] = 1
                    next_set.add(ele_check)

                if visit_flg[goal]:
                    break

        if visit_flg[goal]:
            break

        now_set = next_set
        next_set = set()

    if visit_flg[goal]:
        print(ans)
    else:
        print(-1)












if __name__ == '__main__':
    main()