Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
uv = [list(map(int, input().split())) for _ in range(m)]
alist = [[] for _ in range(n)]
for u, v in uv:
    alist[u-1].append(v-1)
s, t = map(int, input().split())

alist3 = [set([]) for _ in range(n)]
for v in range(n):
    for v1 in alist[v]:
        for v2 in alist[v1]:
            for v3 in alist[v2]:
                alist3[v].add(v3)

# BFS
queue = [s-1]
visited = [-1]*n
visited[s-1] = 0
while len(queue) > 0:
    q = queue.pop(0)
    for v in alist3[q]:
        if visited[v] < 0:
            queue.append(v)
            visited[v] = visited[q] + 1
print(visited[t-1])




Fixed submission:
n, m = map(int, input().split())
uv = [list(map(int, input().split())) for _ in range(m)]
s, t = map(int, input().split())

alist = [[] for _ in range(3*n)]
for u, v in uv:
    u, v = u-1, v-1
    alist[u].append(n+v)
    alist[n+u].append(2*n+v)
    alist[2*n+u].append(v)


# BFS
queue = [s-1]
visited = [-3]*(3*n)
visited[s-1] = 0
while len(queue) > 0:
    q = queue.pop(0)
    for v in alist[q]:
        if visited[v] < 0:
            queue.append(v)
            visited[v] = visited[q] + 1
print(visited[t-1]//3)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
uv = [list(map(int, input().split())) for _ in range(m)]
alist = [[] for _ in range(n)]
for u, v in uv:
    alist[u-1].append(v-1)
s, t = map(int, input().split())

alist3 = [set([]) for _ in range(n)]
for v in range(n):
    for v1 in alist[v]:
        for v2 in alist[v1]:
            for v3 in alist[v2]:
                alist3[v].add(v3)

# BFS
queue = [s]
visited = [-1]*n
visited[s] = 0
while len(queue) > 0:
    q = queue.pop(0)
    for v in alist3[q]:
        if visited[v] < 0:
            queue.append(v)
            visited[v] = visited[q] + 1
print(visited[t])




Fixed submission:
n, m = map(int, input().split())
uv = [list(map(int, input().split())) for _ in range(m)]
s, t = map(int, input().split())

alist = [[] for _ in range(3*n)]
for u, v in uv:
    u, v = u-1, v-1
    alist[u].append(n+v)
    alist[n+u].append(2*n+v)
    alist[2*n+u].append(v)


# BFS
queue = [s-1]
visited = [-3]*(3*n)
visited[s-1] = 0
while len(queue) > 0:
    q = queue.pop(0)
    for v in alist[q]:
        if visited[v] < 0:
            queue.append(v)
            visited[v] = visited[q] + 1
print(visited[t-1]//3)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
uv = [list(map(int, input().split())) for _ im range(m)]
alist = [[] for _ in range(n)]
for u, v in uv:
    alist[u].append(v)
s, t = map(int, input().split())

alist3 = [set([]) for _ in range(n)]
for u, v in uv:
for v in range(n):
    for v1 in alist[v]:
        for v2 in alist[v1]:
            for v3 in alist[v2]:
                alist3[v].add(v2)

# BFS
queue = [s]
visited = [-1]*n
visited[s] = 0
while len(queue) > 0:
    q = queue.pop(0)
    for v in alist3[q]:
        if visited[v] >= 0:
            queue.append(v)
            visited[v] = visited[q] + 1
print(visited[t])




Fixed submission:
n, m = map(int, input().split())
uv = [list(map(int, input().split())) for _ in range(m)]
s, t = map(int, input().split())

alist = [[] for _ in range(3*n)]
for u, v in uv:
    u, v = u-1, v-1
    alist[u].append(n+v)
    alist[n+u].append(2*n+v)
    alist[2*n+u].append(v)


# BFS
queue = [s-1]
visited = [-3]*(3*n)
visited[s-1] = 0
while len(queue) > 0:
    q = queue.pop(0)
    for v in alist[q]:
        if visited[v] < 0:
            queue.append(v)
            visited[v] = visited[q] + 1
print(visited[t-1]//3)