Buggy python submission (verdict: Runtime Error):
def dfs(u,c):
  nc=(c+1)%3
  for v in to[u]:
    if d[v][nc]<d[u][c]+1: continue;
    d[v][nc]=d[u][c]+1
    dfs(v,nc)


def main():
  for i in range(m):
    u,v=map(lambda x:int(x)-1,input().split())
    to[u].append(v)
  s,t=map(lambda x:int(x)-1,input().split())

  d[s][0]=0
  dfs(s,0)
  
  #print(d)
  ans=d[t][0]
  print(ans//3 if ans!=INF else -1)

  
if __name__=='__main__':
  n,m=map(int,input().split())
  INF=10**9
  d=[[INF]*3 for i in range(n)]
  to=[[] for i in range(n)]
  s=t=0
  main()

Fixed submission:
from collections import deque

def main():

  n,m=map(int,input().split())
  INF=10**9
  d=[[INF]*3 for i in range(n)]
  to=[[] for i in range(n)]
  for i in range(m):
    u,v=map(lambda x:int(x)-1,input().split())
    to[u].append(v)
  s,t=map(lambda x:int(x)-1,input().split())

  #print(to)
  # calc d (after 3 times dist)
  d[s][0]=0
  cnt=0
  q=deque([(s,cnt)])
  while q:
    u,c=q.popleft()
    for v in to[u]:
      nc=(c+1)%3
      if d[v][nc]!=INF: continue;
      d[v][nc]=d[u][c]+1
      q.append((v,nc))
      
  #print(d)
  ans=d[t][0]
  print(ans//3 if ans!=INF else -1)

  
if __name__=='__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix

def main():

  n,m=map(int,input().split())
  d=[[0 for i in range(n)] for i in range(n)]
  to=[[] for i in range(n)]
  for i in range(m):
    u,v=map(lambda x:int(x)-1,input().split())
    to[u].append(v)
  s,t=map(lambda x:int(x)-1,input().split())

  #print(to)

  # calc d (after 3 times dist)
  for i in range(n):
    first,second,third=[],[],[]
    for j in to[i]:
      first.append(j)
    for j in first:
      for k in to[j]:
        second.append(k)
    for j in second:
      for k in to[j]:
        third.append(k)
        d[i][k]=1

  ans=dijkstra(csr_matrix(d),indices=0).astype(int)
  print(ans[t] if ans[t]>0 else -1)


if __name__=='__main__':
  main()

Fixed submission:
from collections import deque

def main():

  n,m=map(int,input().split())
  INF=10**9
  d=[[INF]*3 for i in range(n)]
  to=[[] for i in range(n)]
  for i in range(m):
    u,v=map(lambda x:int(x)-1,input().split())
    to[u].append(v)
  s,t=map(lambda x:int(x)-1,input().split())

  #print(to)
  # calc d (after 3 times dist)
  d[s][0]=0
  cnt=0
  q=deque([(s,cnt)])
  while q:
    u,c=q.popleft()
    for v in to[u]:
      nc=(c+1)%3
      if d[v][nc]!=INF: continue;
      d[v][nc]=d[u][c]+1
      q.append((v,nc))
      
  #print(d)
  ans=d[t][0]
  print(ans//3 if ans!=INF else -1)

  
if __name__=='__main__':
  main()

Buggy python submission (verdict: Runtime Error):
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix

def main():

  n,m=map(int,input().split())
  d=[[0 for i in range(n)] for i in range(n)]
  to=[[] for i in range(n)]
  for i in range(m):
    u,v=map(lambda x:int(x)-1,input().split())
    to[u].append(v)
  s,t=map(lambda x:int(x)-1,input().split())

  #print(to)

  # calc d (after 3 times dist)
  for i in range(n):
    first,second,third=[],[],[]
    for j in to[i]:
      first.append(j)
    for j in first:
      for k in to[j]:
        second.append(k)
    for j in second:
      for k in to[j]:
        third.append(k)
        d[i][k]=1

  ans=dijkstra(csr_matrix(d),indices=0).astype(int)
  print(ans[t] if ans[t]>0 else -1)


if __name__=='__main__':
  main()

Fixed submission:
from collections import deque

def main():

  n,m=map(int,input().split())
  INF=10**9
  d=[[INF]*3 for i in range(n)]
  to=[[] for i in range(n)]
  for i in range(m):
    u,v=map(lambda x:int(x)-1,input().split())
    to[u].append(v)
  s,t=map(lambda x:int(x)-1,input().split())

  #print(to)
  # calc d (after 3 times dist)
  d[s][0]=0
  cnt=0
  q=deque([(s,cnt)])
  while q:
    u,c=q.popleft()
    for v in to[u]:
      nc=(c+1)%3
      if d[v][nc]!=INF: continue;
      d[v][nc]=d[u][c]+1
      q.append((v,nc))
      
  #print(d)
  ans=d[t][0]
  print(ans//3 if ans!=INF else -1)

  
if __name__=='__main__':
  main()