Buggy python submission (verdict: Time Limit Exceeded):
import queue

N,M = map(int, input().split())
G = [[] for i in range(3*N+1)]

for i in range(M):
  u,v = map(int, input().split())
  G[u].append(v+N)
  G[u+N].append(v+2*N)
  G[u+2*N].append(v)
S,T = map(int, input().split())

def bfs(s):
  que = queue.Queue()
  que.put(s)
  d = [-1 for _ in range(3*N+1)]
  d[s] = 0

  while not que.empty():

    p = que.get()
    if (T == p):
      return d[p]

    for pp in G[p]:
      if d[pp] == -1:
        que.put(pp)
        d[pp] = d[p]+1

  return -1

ans = bfs(S)
if (-1 == ans):
  print(-1)
else:
  print(ans//3)


Fixed submission:
import collections

N,M = map(int, input().split())
G = [[] for i in range(3*N+1)]

for i in range(M):
  u,v = map(int, input().split())
  G[u].append(v+N)
  G[u+N].append(v+2*N)
  G[u+2*N].append(v)
S,T = map(int, input().split())

def bfs(s):
  que = collections.deque()
  que.append(s)
  d = [-1 for _ in range(3*N+1)]
  d[s] = 0

  while (0 < len(que)):

    p = que.popleft()
    if (T == p):
      return d[p]

    for pp in G[p]:
      if d[pp] == -1:
        que.append(pp)
        d[pp] = d[p]+1

  return -1

ans = bfs(S)
if (-1 == ans):
  print(-1)
else:
  print(ans//3)

Buggy python submission (verdict: Time Limit Exceeded):
import queue

N,M = map(int, input().split())
dst = [[] for i in range(N+1)]

for i in range(M):
  u,v = map(int, input().split())
  dst[u].append(v)
S,T = map(int, input().split())

dst2 = [[] for i in range(N+1)]

for i in range(1,N+1):
  for s0 in dst[i]:
    for s1 in dst[s0]:
      for s2 in dst[s1]:
        if i != s2:
          if not s2 in dst2[i]:
            dst2[i].append(s2)
  
def bfs(s):
  que = queue.Queue()
  que.put(s)
  d = [-1 for _ in range(N+1)]
  d[s] = 0

  while not que.empty():

    p = que.get()
    if (T == p):
      return d[p]

    for pp in dst2[p]:
      if d[pp] == -1:
        que.put(pp)
        d[pp] = d[p]+1

  return -1

ans = bfs(S)
print(ans)


Fixed submission:
import collections

N,M = map(int, input().split())
G = [[] for i in range(3*N+1)]

for i in range(M):
  u,v = map(int, input().split())
  G[u].append(v+N)
  G[u+N].append(v+2*N)
  G[u+2*N].append(v)
S,T = map(int, input().split())

def bfs(s):
  que = collections.deque()
  que.append(s)
  d = [-1 for _ in range(3*N+1)]
  d[s] = 0

  while (0 < len(que)):

    p = que.popleft()
    if (T == p):
      return d[p]

    for pp in G[p]:
      if d[pp] == -1:
        que.append(pp)
        d[pp] = d[p]+1

  return -1

ans = bfs(S)
if (-1 == ans):
  print(-1)
else:
  print(ans//3)

Buggy python submission (verdict: Time Limit Exceeded):
import queue

N,M = map(int, input().split())
dst = [[] for i in range(N+1)]

for i in range(M):
  u,v = map(int, input().split())
  dst[u].append(v)
S,T = map(int, input().split())

dst2 = [[] for i in range(N+1)]

for i in range(1,N+1):
  for s0 in dst[i]:
    for s1 in dst[s0]:
      for s2 in dst[s1]:
        if i != s2:
          if not s2 in dst2[i]:
            dst2[i].append(s2)
  
def bfs(s):
  que = queue.Queue()
  que.put(s)
  d = [-1 for _ in range(N+1)]
  d[s] = 0

  while not que.empty():

    p = que.get()
    if (T == p):
      return d[p]

    for pp in dst2[p]:
      if d[pp] == -1:
        que.put(pp)
        d[pp] = d[p]+1

  return -1

ans = bfs(S)
print(ans)


Fixed submission:
import collections

N,M = map(int, input().split())
G = [[] for i in range(3*N+1)]

for i in range(M):
  u,v = map(int, input().split())
  G[u].append(v+N)
  G[u+N].append(v+2*N)
  G[u+2*N].append(v)
S,T = map(int, input().split())

def bfs(s):
  que = collections.deque()
  que.append(s)
  d = [-1 for _ in range(3*N+1)]
  d[s] = 0

  while (0 < len(que)):

    p = que.popleft()
    if (T == p):
      return d[p]

    for pp in G[p]:
      if d[pp] == -1:
        que.append(pp)
        d[pp] = d[p]+1

  return -1

ans = bfs(S)
if (-1 == ans):
  print(-1)
else:
  print(ans//3)