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


def reachable_by_3steps_from(node, edges):
    dist1 = edges[node]
    reachable = []
    for d1 in dist1:
        dist2 = edges[d1]
        for d2 in dist2:
            reachable += edges[d2]
    return reachable


n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    _from, to = map(int, input().split())
    edges[_from-1].append(to-1)

start, goal = map(int, input().split())
skip_path = [[] for _ in range(n)]
for i in range(n):
    skip_path[i] = reachable_by_3steps_from(i, edges)

todo = deque([])
seen = [False] * n
seen[start-1] = True
for to in skip_path[start-1]:
    todo.append([to, 1])
    if to == goal-1:
        print(1)
        exit()

while todo:
    node, dist = todo.popleft()
    if seen[node]:
        continue
    seen[node] = True
    for to in skip_path[node]:
        if seen[to]:
            continue
        todo.append([to, dist+1])
        if to == goal-1:
            print(dist+1)
            exit()

print(-1)

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


n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    _from, to = map(int, input().split())
    edges[_from-1].append(to-1)

start, goal = map(int, input().split())

todo = deque([])
seen = [[False] * n for i in range(3)]
seen[0][start-1] = True
for to in edges[start-1]:
    dim = 1
    dist = 1
    todo.append([to, dim, dist])

while todo:
    node, dim, dist = todo.popleft()
    if seen[dim][node]:
        continue
    seen[dim][node] = True
    dim += 1
    if dim == 3:
        dim = 0
    for to in edges[node]:
        if seen[dim][to]:
            continue
        todo.append([to, dim, dist+1])
        if to == goal-1 and dim == 0:
            print((dist+1)//3)
            exit()

print(-1)

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


def reachable_by_3steps_from(node, edges):
    dist1 = edges[node]
    dist2 = []
    for d1 in dist1:
        dist2 += edges[d1]
    reachable = []
    for d2 in dist2:
        reachable += edges[d2]
    return reachable


n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    _from, to = map(int, input().split())
    edges[_from-1].append(to-1)

start, goal = map(int, input().split())
skip_path = [[] for _ in range(n)]
for i in range(n):
    skip_path[i] = reachable_by_3steps_from(i, edges)

todo = deque([])
seen = [False] * n
seen[start-1] = True
for to in skip_path[start-1]:
    todo.append([to, 1])
    if to == goal-1:
        print(1)
        exit()

while todo:
    node, dist = todo.popleft()
    if seen[node]:
        continue
    seen[node] = True
    for to in skip_path[node]:
        todo.append([to, dist+1])
        if to == goal-1:
            print(dist+1)
            exit()

print(-1)

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


n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    _from, to = map(int, input().split())
    edges[_from-1].append(to-1)

start, goal = map(int, input().split())

todo = deque([])
seen = [[False] * n for i in range(3)]
seen[0][start-1] = True
for to in edges[start-1]:
    dim = 1
    dist = 1
    todo.append([to, dim, dist])

while todo:
    node, dim, dist = todo.popleft()
    if seen[dim][node]:
        continue
    seen[dim][node] = True
    dim += 1
    if dim == 3:
        dim = 0
    for to in edges[node]:
        if seen[dim][to]:
            continue
        todo.append([to, dim, dist+1])
        if to == goal-1 and dim == 0:
            print((dist+1)//3)
            exit()

print(-1)

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


def reachable_by_3steps_from(node, edges):
    dist1 = edges[node]
    dist2 = []
    for d1 in dist1:
        dist2 += edges[d1]
    reachable = []
    for d2 in dist2:
        reachable += edges[d2]
    return reachable


n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    _from, to = map(int, input().split())
    edges[_from-1].append(to-1)

start, goal = map(int, input().split())
skip_path = [[] for _ in range(n)]
for i in range(n):
    skip_path[i] = reachable_by_3steps_from(i, edges)

todo = deque([])
seen = [False] * n
seen[start-1] = True
for to in skip_path[start-1]:
    todo.append([to, 1])
    if to == goal-1:
        print(1)
        exit()

while todo:
    node, dist = todo.popleft()
    if seen[node]:
        continue
    seen[node] = True
    for to in skip_path[node]:
        todo.append([to, dist+1])
        if to == goal-1:
            print(dist+1)
            exit()

print(-1)

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


n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    _from, to = map(int, input().split())
    edges[_from-1].append(to-1)

start, goal = map(int, input().split())

todo = deque([])
seen = [[False] * n for i in range(3)]
seen[0][start-1] = True
for to in edges[start-1]:
    dim = 1
    dist = 1
    todo.append([to, dim, dist])

while todo:
    node, dim, dist = todo.popleft()
    if seen[dim][node]:
        continue
    seen[dim][node] = True
    dim += 1
    if dim == 3:
        dim = 0
    for to in edges[node]:
        if seen[dim][to]:
            continue
        todo.append([to, dim, dist+1])
        if to == goal-1 and dim == 0:
            print((dist+1)//3)
            exit()

print(-1)