Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int, input().split())
g = {}
for v in range(1,N*3+1):
    g[v] = []

for _ in range(M):
    u,v = map(int, input().split())
    g[u + N*0].append(v + N*1)
    g[u + N*1].append(v + N*2)
    g[u + N*2].append(v + N*0)

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

visiting = [(S,0)]
visited = {}
while visiting:
    v,d = visiting.pop(0)
    if v in visited:
        continue
    visited[v] = d
    if v == T:
        break

    for v2 in g[v]:
        if v2 in visiting or v2 in visited:
            continue
        visiting.append((v2,d+1))

if T in visited:
    print(visited[T] // 3)
else:
    print(-1)


Fixed submission:
N, M = map(int, input().split())
E = {}
for v in range(1,N*3+1):
    E[v] = []
for _ in range(M):
    u, v = map(int, input().split())
    E[u].append(v+N)
    E[u+N].append(v+2*N)
    E[u+2*N].append(v)
S, T = map(int, input().split())
dist = [-1 for _ in range(3*N)]
dist[S-1] = 0

q = []
q.append(S)
while q:
    n = q.pop(0)
    if n == T:
        break
    for t in E[n]:
        if dist[t-1] < 0:
            dist[t-1] = dist[n-1] + 1
            q.append(t)
print(-1 if dist[T-1] < 0 else dist[T-1] // 3)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int, input().split())

g = {}
steps = 3
for v in range(1,N*steps+1):
    g[v] = []

for _ in range(M):
    u,v = map(int, input().split())
    g[u + N*0].append(v + N*1)
    g[u + N*1].append(v + N*2)
    g[u + N*2].append(v + N*0)

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

visiting = [(S,0)]
visited = {}
while visiting:
    v,d = visiting.pop(0)
    if v in visited:
        continue
    visited[v] = d
    if v == T:
        break

    for v2 in g.get(v,()):
        if v2 in visiting or v2 in visited:
            continue
        visiting.append((v2,d+1))
if T in visited:
    print(visited[T] // steps)
else:
    print(-1)


Fixed submission:
N, M = map(int, input().split())
E = {}
for v in range(1,N*3+1):
    E[v] = []
for _ in range(M):
    u, v = map(int, input().split())
    E[u].append(v+N)
    E[u+N].append(v+2*N)
    E[u+2*N].append(v)
S, T = map(int, input().split())
dist = [-1 for _ in range(3*N)]
dist[S-1] = 0

q = []
q.append(S)
while q:
    n = q.pop(0)
    if n == T:
        break
    for t in E[n]:
        if dist[t-1] < 0:
            dist[t-1] = dist[n-1] + 1
            q.append(t)
print(-1 if dist[T-1] < 0 else dist[T-1] // 3)

Buggy python submission (verdict: Runtime Error):
N,M = map(int, input().split())

g = {}
steps = 3
for v in range(1,N*steps+1):
    g[v] = []

for _ in range(M):
    u,v = map(int, input().split())
	g[u + N*0].append(v + N*1)
    g[u + N*1].append(v + N*2)
    g[u + N*2].append(v + N*0)

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

visiting = [(S,0)]
visited = {}
while visiting:
    v,d = visiting.pop(0)
    if v in visited:
        continue
    visited[v] = d
    if v == T:
        break

    for v2 in g.get(v,()):
        if v2 in visiting or v2 in visited:
            continue
        visiting.append((v2,d+1))
if T in visited:
    print(visited[T] // steps)
else:
    print(-1)


Fixed submission:
N, M = map(int, input().split())
E = {}
for v in range(1,N*3+1):
    E[v] = []
for _ in range(M):
    u, v = map(int, input().split())
    E[u].append(v+N)
    E[u+N].append(v+2*N)
    E[u+2*N].append(v)
S, T = map(int, input().split())
dist = [-1 for _ in range(3*N)]
dist[S-1] = 0

q = []
q.append(S)
while q:
    n = q.pop(0)
    if n == T:
        break
    for t in E[n]:
        if dist[t-1] < 0:
            dist[t-1] = dist[n-1] + 1
            q.append(t)
print(-1 if dist[T-1] < 0 else dist[T-1] // 3)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int, input().split())

g = {}
steps = 3
for v in range(1,N*steps+1):
    g[v] = []

for _ in range(M):
    u,v = map(int, input().split())

    for idx in range(steps):
        g[u + N*idx].append(v + N*((idx+1) % steps))

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

visiting = [(S,0)]
visited = {}
while visiting:
    v,d = visiting.pop(0)
    if v in visited:
        continue
    visited[v] = d
    if v == T:
        break

    for v2 in g.get(v,()):
        if v2 in visiting or v2 in visited:
            continue
        visiting.append((v2,d+1))
if T in visited:
    print(visited[T] // steps)
else:
    print(-1)


Fixed submission:
N, M = map(int, input().split())
E = {}
for v in range(1,N*3+1):
    E[v] = []
for _ in range(M):
    u, v = map(int, input().split())
    E[u].append(v+N)
    E[u+N].append(v+2*N)
    E[u+2*N].append(v)
S, T = map(int, input().split())
dist = [-1 for _ in range(3*N)]
dist[S-1] = 0

q = []
q.append(S)
while q:
    n = q.pop(0)
    if n == T:
        break
    for t in E[n]:
        if dist[t-1] < 0:
            dist[t-1] = dist[n-1] + 1
            q.append(t)
print(-1 if dist[T-1] < 0 else dist[T-1] // 3)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int, input().split())

# N is the number of vertices
# M is the number of edges

g = {}
for v in range(1, N+1):
    g[v] = []

for _ in range(M):
    u,v = map(int, input().split())
    g[u].append(v)

S,T = map(int, input().split())
# S is start
# T is target


def move(start, steps):
    current_locations = set()
    next_locations = set()
    current_locations.add(start)

    for _ in range(steps):
        nxt = set()
        for v in current_locations:
            for v2 in g.get(v, ()):
                nxt.add(v2)
        current_locations = nxt

    return current_locations

visiting = [(S,0)]
visited = {}

while visiting:
    v,kkp = visiting.pop(0)
    if v in visited:
        continue

    visited[v] = kkp
    if v == T:
        break

    for v2 in move(v,3):
        if v2 in visiting or v2 in visited:
            continue
        visiting.append((v2,kkp+1))

if T in visited:
    print(visited[T])
else:
    print(-1)


Fixed submission:
N, M = map(int, input().split())
E = {}
for v in range(1,N*3+1):
    E[v] = []
for _ in range(M):
    u, v = map(int, input().split())
    E[u].append(v+N)
    E[u+N].append(v+2*N)
    E[u+2*N].append(v)
S, T = map(int, input().split())
dist = [-1 for _ in range(3*N)]
dist[S-1] = 0

q = []
q.append(S)
while q:
    n = q.pop(0)
    if n == T:
        break
    for t in E[n]:
        if dist[t-1] < 0:
            dist[t-1] = dist[n-1] + 1
            q.append(t)
print(-1 if dist[T-1] < 0 else dist[T-1] // 3)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int, input().split())

# N is the number of vertices
# M is the number of edges

g = {}
for v in range(1, N+1):
    g[v] = []

for _ in range(M):
    u,v = map(int, input().split())
    g[u].append(v)

S,T = map(int, input().split())
# S is start
# T is target


def move(start, steps):
    current_locations = set()
    next_locations = set()
    current_locations.add(start)

    for _ in range(steps):
        nxt = set()
        for v in current_locations:
            for v2 in g.get(v, ()):
                nxt.add(v2)
        current_locations = nxt

    return current_locations

visiting = [(S,0)]
visited = {}

while visiting:
    v,kkp = visiting.pop(0)
    if v in visited:
        continue

    visited[v] = kkp
    if v == T:
        break

    for v2 in move(v,3):
        if v2 in visiting or v2 in visited:
            continue
        visiting.append((v2,kkp+1))

if T in visited:
    print(visited[T])
else:
    print(-1)


Fixed submission:
N, M = map(int, input().split())
E = {}
for v in range(1,N*3+1):
    E[v] = []
for _ in range(M):
    u, v = map(int, input().split())
    E[u].append(v+N)
    E[u+N].append(v+2*N)
    E[u+2*N].append(v)
S, T = map(int, input().split())
dist = [-1 for _ in range(3*N)]
dist[S-1] = 0

q = []
q.append(S)
while q:
    n = q.pop(0)
    if n == T:
        break
    for t in E[n]:
        if dist[t-1] < 0:
            dist[t-1] = dist[n-1] + 1
            q.append(t)
print(-1 if dist[T-1] < 0 else dist[T-1] // 3)