Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n, m = list(map(int, input().split()))
graph = defaultdict(list)
for i in range(m):
  u, v = list(map(int, input().split()))
  graph[u].append(v+n)
  graph[u+n].append(v+2*n)
  graph[u+2*n].append(v)
s, t = list(map(int, input().split()))
dist = [-1 for i in range(3*n+10)]

def bfs():
  que = [(s, 0)]
  while len(que) > 0:
    cur, cost = que.pop(0)
    dist[cur] = cost
    for to in graph[cur]:
      if dist[to] == -1:
        que.append((to, cost+1))

bfs()
print(dist[t]//3)

Fixed submission:
from collections import defaultdict, deque
n, m = list(map(int, input().split()))
graph = defaultdict(list)
for i in range(m):
  u, v = list(map(int, input().split()))
  graph[u].append(v+n)
  graph[u+n].append(v+2*n)
  graph[u+2*n].append(v)
s, t = list(map(int, input().split()))
dist = [-1 for i in range(3*n+10)]

def bfs():
  que = deque([s])
  dist[s] = 0
  while len(que) > 0:
    cur = que.popleft()
    for to in graph[cur]:
      if dist[to] == -1:
        dist[to] = dist[cur]+1
        que.append(to)

bfs()
print(dist[t]//3)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n, m = list(map(int, input().split()))
graph = defaultdict(set)
for i in range(m):
  u, v = list(map(int, input().split()))
  graph[u].add(v+n)
  graph[u+n].add(v+2*n)
  graph[u+2*n].add(v)
s, t = list(map(int, input().split()))
dist = [-1 for i in range(3*n+10)]

def bfs():
  que = [(s, 0)]
  while len(que) > 0:
    cur, cost = que.pop(0)
    if dist[cur] != -1:
      continue
    dist[cur] = cost
    for to in graph[cur]:
      que.append((to, cost+1))

bfs()
print(dist[t]//3)

Fixed submission:
from collections import defaultdict, deque
n, m = list(map(int, input().split()))
graph = defaultdict(list)
for i in range(m):
  u, v = list(map(int, input().split()))
  graph[u].append(v+n)
  graph[u+n].append(v+2*n)
  graph[u+2*n].append(v)
s, t = list(map(int, input().split()))
dist = [-1 for i in range(3*n+10)]

def bfs():
  que = deque([s])
  dist[s] = 0
  while len(que) > 0:
    cur = que.popleft()
    for to in graph[cur]:
      if dist[to] == -1:
        dist[to] = dist[cur]+1
        que.append(to)

bfs()
print(dist[t]//3)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n, m = list(map(int, input().split()))
graph = defaultdict(set)
for i in range(m):
  u, v = list(map(int, input().split()))
  graph[u].add(v)
s, t = list(map(int, input().split()))
dist = [[-1 for i in range(3)] for i in range(10**5+10)]

def bfs():
  que = [(s, 0, 0)] #(x, cost, kenkenpa)
  while len(que) > 0:
    p = que.pop(0)
    if dist[p[0]][p[2]] != -1:
      continue
    dist[p[0]][p[2]] = p[1]
    for nx in graph[p[0]]:
      que.append((nx, p[1]+1, (p[2]+1)%3))

bfs()
print(dist[t][0]//3)

Fixed submission:
from collections import defaultdict, deque
n, m = list(map(int, input().split()))
graph = defaultdict(list)
for i in range(m):
  u, v = list(map(int, input().split()))
  graph[u].append(v+n)
  graph[u+n].append(v+2*n)
  graph[u+2*n].append(v)
s, t = list(map(int, input().split()))
dist = [-1 for i in range(3*n+10)]

def bfs():
  que = deque([s])
  dist[s] = 0
  while len(que) > 0:
    cur = que.popleft()
    for to in graph[cur]:
      if dist[to] == -1:
        dist[to] = dist[cur]+1
        que.append(to)

bfs()
print(dist[t]//3)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n, m = list(map(int, input().split()))
graph = defaultdict(set)
for i in range(m):
  u, v = list(map(int, input().split()))
  graph[u].add(v)
s, t = list(map(int, input().split()))
dist = [[-1 for i in range(3)] for i in range(10**5+10)]

def bfs():
  que = [(s, 0, 0)] #(x, cost, kenkenpa)
  while len(que) > 0:
    p = que.pop(0)
    if dist[p[0]][p[2]] != -1:
      continue
    dist[p[0]][p[2]] = p[1]
    for nx in graph[p[0]]:
      que.append((nx, p[1]+1, (p[2]+1)%3))

bfs()
print(dist[t][0]//3)

Fixed submission:
from collections import defaultdict, deque
n, m = list(map(int, input().split()))
graph = defaultdict(list)
for i in range(m):
  u, v = list(map(int, input().split()))
  graph[u].append(v+n)
  graph[u+n].append(v+2*n)
  graph[u+2*n].append(v)
s, t = list(map(int, input().split()))
dist = [-1 for i in range(3*n+10)]

def bfs():
  que = deque([s])
  dist[s] = 0
  while len(que) > 0:
    cur = que.popleft()
    for to in graph[cur]:
      if dist[to] == -1:
        dist[to] = dist[cur]+1
        que.append(to)

bfs()
print(dist[t]//3)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n, m = list(map(int, input().split()))
d1 = defaultdict(set)
for i in range(m):
  u, v = list(map(int, input().split()))
  d1[u].add(v)
s, t = list(map(int, input().split()))
d2 = defaultdict(set)

def dfs (x, nx, cnt, visited):
  if nx in visited:
    return
  if cnt==3:
    d2[x].add(nx)
  elif cnt>3:
    return

  for nnx in d1[nx]:
    dfs(x, nnx, cnt+1, visited)
  visited.add(nx)

for i in range(1, n+1):
  visited = set()
  dfs(i, i, 0, visited)
  visited.add(i)

def bfs():
  que = [(s, 0)]
  visited = set()

  while len(que) > 0:
    p = que.pop(0)
    if p[0]==t:
      return p[1]
    for nx in d2[p[0]]:
      if nx in visited:
        continue
      visited.add(nx)
      que.append((nx, p[1]+1))
  return -1

print(bfs())

Fixed submission:
from collections import defaultdict, deque
n, m = list(map(int, input().split()))
graph = defaultdict(list)
for i in range(m):
  u, v = list(map(int, input().split()))
  graph[u].append(v+n)
  graph[u+n].append(v+2*n)
  graph[u+2*n].append(v)
s, t = list(map(int, input().split()))
dist = [-1 for i in range(3*n+10)]

def bfs():
  que = deque([s])
  dist[s] = 0
  while len(que) > 0:
    cur = que.popleft()
    for to in graph[cur]:
      if dist[to] == -1:
        dist[to] = dist[cur]+1
        que.append(to)

bfs()
print(dist[t]//3)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n, m = list(map(int, input().split()))
d1 = defaultdict(set)
for i in range(m):
  u, v = list(map(int, input().split()))
  d1[u].add(v)
s, t = list(map(int, input().split()))
d2 = defaultdict(set)

def dfs (x, nx, cnt, visited):
  if nx in visited:
    return
  if cnt==3:
    d2[x].add(nx)
  elif cnt>3:
    return

  for nnx in d1[nx]:
    dfs(x, nnx, cnt+1, visited)
  visited.add(nx)

for i in range(1, n+1):
  visited = set()
  dfs(i, i, 0, visited)
  visited.add(i)

from heapq import heappop, heappush
prev = [-1 for _ in range(n+1)]
def dijkstra(s, graph):
  d = [float("INF") for _ in range(n+1)]
  d[s] = 0
  q = []
  heappush(q, [0, s])

  while q:
    dt, v = heappop(q)
    if d[v] < dt:
      continue
    for to in graph[v]:
      if d[to] > dt+1:
        d[to] = dt+1
        prev[to] = v
        heappush(q, [d[to], to])
  return d
dd = dijkstra(s, d2)
if dd[t]==float("INF"):
  print(-1)
else:
  print(dd[t])

Fixed submission:
from collections import defaultdict, deque
n, m = list(map(int, input().split()))
graph = defaultdict(list)
for i in range(m):
  u, v = list(map(int, input().split()))
  graph[u].append(v+n)
  graph[u+n].append(v+2*n)
  graph[u+2*n].append(v)
s, t = list(map(int, input().split()))
dist = [-1 for i in range(3*n+10)]

def bfs():
  que = deque([s])
  dist[s] = 0
  while len(que) > 0:
    cur = que.popleft()
    for to in graph[cur]:
      if dist[to] == -1:
        dist[to] = dist[cur]+1
        que.append(to)

bfs()
print(dist[t]//3)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n, m = list(map(int, input().split()))
d1 = defaultdict(list)
for i in range(m):
  u, v = list(map(int, input().split()))
  d1[u].append(v)
s, t = list(map(int, input().split()))
d2 = defaultdict(set)

def dfs (x, nx, cnt):
  if cnt==3:
    d2[x].add(nx)
  elif cnt>3:
    return

  for nnx in d1[nx]:
    dfs(x, nnx, cnt+1)

for i in range(1, n+1):
  dfs(i, i, 0)

from heapq import heappop, heappush
prev = [-1 for _ in range(n+1)]
def dijkstra(s, graph):
  d = [float("INF") for _ in range(n+1)]
  d[s] = 0
  q = []
  heappush(q, [0, s])

  while q:
    dt, v = heappop(q)
    if d[v] < dt:
      continue
    for to in graph[v]:
      if d[to] > dt+1:
        d[to] = dt+1
        prev[to] = v
        heappush(q, [d[to], to])
  return d
dd = dijkstra(s, d2)
if dd[t]==float("INF"):
  print(-1)
else:
  print(dd[t])

Fixed submission:
from collections import defaultdict, deque
n, m = list(map(int, input().split()))
graph = defaultdict(list)
for i in range(m):
  u, v = list(map(int, input().split()))
  graph[u].append(v+n)
  graph[u+n].append(v+2*n)
  graph[u+2*n].append(v)
s, t = list(map(int, input().split()))
dist = [-1 for i in range(3*n+10)]

def bfs():
  que = deque([s])
  dist[s] = 0
  while len(que) > 0:
    cur = que.popleft()
    for to in graph[cur]:
      if dist[to] == -1:
        dist[to] = dist[cur]+1
        que.append(to)

bfs()
print(dist[t]//3)