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

def main():
    N, M = map(int, input().split())
    edge = [[] * N for _ in range(N)]
    new_edge = [set() for _ in range(N)]
    for _ in range(M):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        edge[u].append(v)
    
    def dfs(s,now,cnt):
        if cnt == 3:
            if s == now:
                return
            new_edge[s].add(now)
            return

        for v in edge[now]:
            dfs(s,v,cnt+1)
    
    for i in range(N):
        dfs(i,i,0)
    S, T = map(int, input().split())
    S -= 1
    T -= 1
    q = deque([S])
    d = [float('inf')] * N
    d[S] = 0
    while q:
        s = q.popleft()
        if s == T:
            print(d[T])
            exit()
        for v in new_edge[s]: 
            if d[v] != float('inf'):
                continue
            d[v] = d[s] + 1
            q.append(v)

    print(-1)

if __name__ == "__main__":
    main()


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


def main():
    N, M = map(int, input().split())
    edge = [[] for _ in range(N)]
    for _ in range(M):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        edge[u].append(v)

    S, T = map(int, input().split())
    S -= 1
    T -= 1
    dist = [[float('inf')] * 3 for _ in range(N)]
    dist[S][0] = 0
    q = deque([])
    q.append((S,0))
    while q:
        v, l = q.popleft()
        nl = (l + 1) % 3
        for nv in edge[v]:
            if dist[nv][nl] != float('inf'):
                continue
            dist[nv][nl] = dist[v][l] + 1
            q.append((nv,nl))
        
    
    if dist[T][0] == float('inf'):
        print(-1)
    else:
        print(dist[T][0] // 3)




if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import sys

input = sys.stdin.readline

def dijkstra_heap(s,edge,n):
    #始点sから各頂点への最短距離
    d = [float('inf')] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

def main():
    N, M = map(int, input().split())
    edge = [[] * N for _ in range(N)]
    new_edge = [[] * N for _ in range(N)]
    for _ in range(M):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        edge[u].append(v)
    
    def dfs(s,now,cnt):
        if cnt == 3:
            new_edge[s].append((1,now))
            return

        for v in edge[now]:
            dfs(s,v,cnt+1)
    
    for i in range(N):
        dfs(i,i,0)
    S, T = map(int, input().split())
    S -= 1
    T -= 1
    d = dijkstra_heap(S,new_edge,N)
    if d[T] == float('inf'):
        print(-1)
    else:
        print(d[T])
    



if __name__ == "__main__":
    main()

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


def main():
    N, M = map(int, input().split())
    edge = [[] for _ in range(N)]
    for _ in range(M):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        edge[u].append(v)

    S, T = map(int, input().split())
    S -= 1
    T -= 1
    dist = [[float('inf')] * 3 for _ in range(N)]
    dist[S][0] = 0
    q = deque([])
    q.append((S,0))
    while q:
        v, l = q.popleft()
        nl = (l + 1) % 3
        for nv in edge[v]:
            if dist[nv][nl] != float('inf'):
                continue
            dist[nv][nl] = dist[v][l] + 1
            q.append((nv,nl))
        
    
    if dist[T][0] == float('inf'):
        print(-1)
    else:
        print(dist[T][0] // 3)




if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import sys
sys.setrecursionlimit(10 ** 5)
input = sys.stdin.readline

def dijkstra_heap(s,edge,n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

def main():
    N, M = map(int, input().split())
    edge = [[] for _ in range(N)]
    U = set()
    for _ in range(M):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        U.add(u)
        edge[u].append(v)

    S, T = map(int, input().split())
    S -= 1
    T -= 1
    search = [False] * N
    can = [[] for _ in range(N)]
    def dfs(s,p,d):
        if d == 3:
            can[s].append((1,p))
            search[s] = True
            return
        for e in edge[p]:
            dfs(s,e,d+1)

        return

    for u in U:
        if search[u]:
            continue
        dfs(u,u,0)
    d = dijkstra_heap(S,can,N)
    if d[T] == 10**20:
        print(-1)
    else:
        print(d[T])




if __name__ == "__main__":
    main()

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


def main():
    N, M = map(int, input().split())
    edge = [[] for _ in range(N)]
    for _ in range(M):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        edge[u].append(v)

    S, T = map(int, input().split())
    S -= 1
    T -= 1
    dist = [[float('inf')] * 3 for _ in range(N)]
    dist[S][0] = 0
    q = deque([])
    q.append((S,0))
    while q:
        v, l = q.popleft()
        nl = (l + 1) % 3
        for nv in edge[v]:
            if dist[nv][nl] != float('inf'):
                continue
            dist[nv][nl] = dist[v][l] + 1
            q.append((nv,nl))
        
    
    if dist[T][0] == float('inf'):
        print(-1)
    else:
        print(dist[T][0] // 3)




if __name__ == "__main__":
    main()