Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin python3
# -*- coding: utf-8 -*-

# 双方向グラフで幅優先探索

from collections import deque

n, m = map(int, input().split())
g = [set([]) for _ in range(n)]
#隣接リストの作成
for i in range(m):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    g[a].add(b)

graph = [set([]) for _ in range(n)]
for i in range(n):
    g1 = set([])
    g2 = set([])
    g3 = set([])
    g1 = g1 | g[i]
    for x in g1:
        g2 = g2 | g[x]
    for x in g2:
        g3 = g3 | g[x]
    graph[i] = g3

s, t = map(int, input().split())
s, t = s-1, t-1

#幅優先探索
q = deque()
level = [-1]*n
q.append(s)
level[s] = 0
while len(q)>0:
    cur = q.popleft()
    lvl = level[cur]
    for i in graph[cur]:
        if level[i]==-1:
            #階層
            level[i] = lvl + 1
            q.append(i)
print(level[t])


Fixed submission:
#!/usr/bin python3
# -*- coding: utf-8 -*-

# 双方向グラフで幅優先探索

from collections import deque

n, m = map(int, input().split())
g = [set([]) for _ in range(n)]
#隣接リストの作成
for i in range(m):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    g[a].add(b)

s, t = map(int, input().split())
s, t = s-1, t-1

#幅優先探索
q = deque()
level = [[-1]*n for i in range(3)]
q.append((s, 0))
level[0][s] = 0
while len(q)>0:
    cur, hp = q.popleft()
    lvl = level[hp][cur]
    for i in g[cur]:
        if level[(hp+1)%3][i]==-1:
            #階層
            level[(hp+1)%3][i] = lvl + 1
            q.append((i, (hp+1)%3))
print(level[0][t]//3)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin python3
# -*- coding: utf-8 -*-

# 双方向グラフで幅優先探索

from collections import deque

n, m = map(int, input().split())
g = [set([]) for _ in range(n)]
#隣接リストの作成
for i in range(m):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    g[a].add(b)

graph = [set([]) for _ in range(n)]
for i in range(n):
    g1 = set([])
    g2 = set([])
    g3 = set([])
    g1 = g1 | g[i]
    for x in g1:
        g2 = g2 | g[x]
    for x in g2:
        g3 = g3 | g[x]
    graph[i] = g3

s, t = map(int, input().split())
s, t = s-1, t-1

#幅優先探索
q = deque()
seen = [False]*n
level = [-1]*n
parent = [-1]*n
q.append(s)
seen[s] = True
level[s] = 0
parent[s] = 0
while len(q)>0:
    cur = q.popleft()
    lvl = level[cur]
    for i in graph[cur]:
        if seen[i]==False:
            seen[i] = True
            #階層
            level[i] = lvl + 1
            #親
            parent[i] = cur
            q.append(i)
print(level[t])


Fixed submission:
#!/usr/bin python3
# -*- coding: utf-8 -*-

# 双方向グラフで幅優先探索

from collections import deque

n, m = map(int, input().split())
g = [set([]) for _ in range(n)]
#隣接リストの作成
for i in range(m):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    g[a].add(b)

s, t = map(int, input().split())
s, t = s-1, t-1

#幅優先探索
q = deque()
level = [[-1]*n for i in range(3)]
q.append((s, 0))
level[0][s] = 0
while len(q)>0:
    cur, hp = q.popleft()
    lvl = level[hp][cur]
    for i in g[cur]:
        if level[(hp+1)%3][i]==-1:
            #階層
            level[(hp+1)%3][i] = lvl + 1
            q.append((i, (hp+1)%3))
print(level[0][t]//3)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin python3
# -*- coding: utf-8 -*-

# 双方向グラフで幅優先探索

from collections import deque

n, m = map(int, input().split())
g = [set([]) for _ in range(n)]
#隣接リストの作成
for i in range(m):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    g[a].add(b)

graph = [set([]) for _ in range(n)]
for i in range(n):
    g1 = set([])
    g2 = set([])
    g3 = set([])
    g1 = g1 | g[i]
    for x in g1:
        g2 = g2 | g[x]
    for x in g2:
        g3 = g3 | g[x]
    graph[i] = g3

s, t = map(int, input().split())
s, t = s-1, t-1

#幅優先探索
q = deque()
seen = [False]*n
level = [-1]*n
parent = [-1]*n
q.append(s)
seen[s] = True
level[s] = 0
parent[s] = 0
while len(q)>0:
    cur = q.popleft()
    lvl = level[cur]
    for i in graph[cur]:
        if seen[i]==False:
            seen[i] = True
            #階層
            level[i] = lvl + 1
            #親
            parent[i] = cur
            q.append(i)
print(level[t])


Fixed submission:
#!/usr/bin python3
# -*- coding: utf-8 -*-

# 双方向グラフで幅優先探索

from collections import deque

n, m = map(int, input().split())
g = [set([]) for _ in range(n)]
#隣接リストの作成
for i in range(m):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    g[a].add(b)

s, t = map(int, input().split())
s, t = s-1, t-1

#幅優先探索
q = deque()
level = [[-1]*n for i in range(3)]
q.append((s, 0))
level[0][s] = 0
while len(q)>0:
    cur, hp = q.popleft()
    lvl = level[hp][cur]
    for i in g[cur]:
        if level[(hp+1)%3][i]==-1:
            #階層
            level[(hp+1)%3][i] = lvl + 1
            q.append((i, (hp+1)%3))
print(level[0][t]//3)