Buggy python submission (verdict: Runtime Error):

# # import itertools

# # # N, K = map(int, input().split())

# # def com(n, r):
# #     a = len(list(itertools.combinations(range(n), r)))
# #     return a


from collections import defaultdict

N, M = map(int, input().split())
G = [[] for _ in range(N)]

for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)

S, T = map(int, input().split())
S -= 1
T -= 1

d = defaultdict(int)
ans = []
def dfs(u, path, num, dis):
    global ans
    path += str(u)
    if len(path) == 4 and d[path] != 0:
        ans.append(10**5)
        return

    if u == T and num % 3 == 0:
        ans.append(int(dis / 3))
        return
    if len(path) == 4:
        d[path] += 1
        path = ""
    for v in G[u]:
        dfs(v, path, num+1, dis+1)

dfs(S, path="", num=0, dis=0)
if len(ans) == 0:
    print(-1)
else:
    a = min(ans)
    if a == 10**5:
        print(-1)
    else:
        print(a)



Fixed submission:
# SからTの長さが3の倍数の最短バスを求める
N, M = map(int, input().split())
G = [[] for _ in range(N)]

for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)

S, T = map(int, input().split())
S -= 1
T -= 1

from collections import deque

def bfs(start, terminal):
    q = deque([(start, 0)])
    color = [[0]*3 for _ in range(N)]
    d = [[-1]*3 for _ in range(N)]
    d[start][0] = 0
    l = 0
    while len(q) > 0:
        u, l = q.popleft()
        nl = (l + 1) % 3
        for v in G[u]:
            if color[v][nl] == 0:
                q.append((v, nl))
                color[v][nl] = 1
                d[v][nl] = d[u][l] + 1
    
    if d[terminal][0] == -1:
        return -1
    else:
        return d[terminal][0] // 3 

print(bfs(S, T))

Buggy python submission (verdict: Runtime Error):

# # import itertools

# # # N, K = map(int, input().split())

# # def com(n, r):
# #     a = len(list(itertools.combinations(range(n), r)))
# #     return a


from collections import defaultdict

N, M = map(int, input().split())
G = [[] for _ in range(N)]

for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)

S, T = map(int, input().split())
S -= 1
T -= 1

d = defaultdict(int)
ans = 0
def dfs(u, path, num, dis):
    global ans
    path += str(u)
    if d[path] != 0:
        ans = -1
        return

    if u == T and num % 3 == 0:
        ans = int(dis / 3)
        return
    if len(path) == 4:
        d[path] += 1
        path = ""
    for v in G[u]:
        dfs(v, path, num+1, dis+1)

dfs(S, path="", num=0, dis=0)
if ans == 0:
    print(-1)
else:
    print(ans)



Fixed submission:
# SからTの長さが3の倍数の最短バスを求める
N, M = map(int, input().split())
G = [[] for _ in range(N)]

for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)

S, T = map(int, input().split())
S -= 1
T -= 1

from collections import deque

def bfs(start, terminal):
    q = deque([(start, 0)])
    color = [[0]*3 for _ in range(N)]
    d = [[-1]*3 for _ in range(N)]
    d[start][0] = 0
    l = 0
    while len(q) > 0:
        u, l = q.popleft()
        nl = (l + 1) % 3
        for v in G[u]:
            if color[v][nl] == 0:
                q.append((v, nl))
                color[v][nl] = 1
                d[v][nl] = d[u][l] + 1
    
    if d[terminal][0] == -1:
        return -1
    else:
        return d[terminal][0] // 3 

print(bfs(S, T))