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

input=sys.stdin.readline

N,M=map(int,input().split())
hen=[[] for i in range(0,N)]
for i in range(0,M):
    u,v=map(int,input().split())
    hen[u-1].append(v-1)

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

triplehen=[[] for i in range(0,N)]
for i in range(0,N):
    q=[i]
    h.heapify(q)
    sub=[]
    h.heapify(sub)
    count=0
    while 3>count:
        while q!=[]:
            x=h.heappop(q)
            for p in hen[x]:
                h.heappush(sub,p)
            if q==[]:
                count+=1
                if count==3:
                    for j in sub:
                        triplehen[i].append(j)
                else:
                    if sub==[]:
                        count=3
                q=sub
                sub=[]
                h.heapify(sub)
                break
triplehen=[list(set(triplehen[i])) for i in range(0,N)]

ans=[-1 for i in range(0,N)]
ans[S-1]=0

q=[]
q.append(S-1)
h.heapify(q)
sub=[]
h.heapify(sub)
count=0
while q!=[]:
    while q!=[]:
        x=h.heappop(q)
        for p in triplehen[x]:
            if ans[p]==-1:
                h.heappush(sub,p)
        if q==[]:
            count+=1
            for j in sub:
                ans[j]=count
            q=sub
            sub=[]
            h.heapify(sub)
            break

print(ans[T-1])


Fixed submission:
import heapq as h

N,M=map(int,input().split())
hen={(i,j):[] for i in range(0,N) for j in range(0,3)}
for i in range(0,M):
    u,v=map(int,input().split())
    hen[(u-1,0)].append((v-1,1))
    hen[(u-1,1)].append((v-1,2))
    hen[(u-1,2)].append((v-1,0))

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

start=(S-1,0)
goal=(T-1,0)

distance={(i,j):-1 for i in range(0,N) for j in range(0,3)}

q=[]
q.append(start)
distance[start]=0
h.heapify(q)
sub=[]
h.heapify(sub)
while q!=[]:
    while q!=[]:
        x=h.heappop(q)
        for p in hen[x]:
            if distance[p]==-1:
                h.heappush(sub,p)
                distance[p]=distance[x]+1
        #何らかの操作
        #ここでsubキューに次に使うものを格納
        if q==[]:
            q=sub
            sub=[]
            h.heapify(sub)
            break

if distance[goal]==-1:
    print(-1)
else:
    print(distance[goal]//3)

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

input=sys.stdin.readline

N,M=map(int,input().split())
hen=[[] for i in range(0,N)]
for i in range(0,M):
    u,v=map(int,input().split())
    hen[u-1].append(v-1)

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

ans=[-1 for i in range(0,N)]
ans[S-1]=0

q=[]
q.append(S-1)
h.heapify(q)
sub=[]
h.heapify(sub)
count=0
flag=1
while flag==1:
    flag=0
    while q!=[]:
        x=h.heappop(q)
        for p in hen[x]:
                h.heappush(sub,p)
        if q==[]:
            count+=1
            if count%3==0:
                for j in sub:
                    if ans[j]==-1:
                        ans[j]=count//3
                        flag=1
            else:
                flag=1
            q=sub
            sub=[]
            h.heapify(sub)
            break

print(ans[T-1])


Fixed submission:
import heapq as h

N,M=map(int,input().split())
hen={(i,j):[] for i in range(0,N) for j in range(0,3)}
for i in range(0,M):
    u,v=map(int,input().split())
    hen[(u-1,0)].append((v-1,1))
    hen[(u-1,1)].append((v-1,2))
    hen[(u-1,2)].append((v-1,0))

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

start=(S-1,0)
goal=(T-1,0)

distance={(i,j):-1 for i in range(0,N) for j in range(0,3)}

q=[]
q.append(start)
distance[start]=0
h.heapify(q)
sub=[]
h.heapify(sub)
while q!=[]:
    while q!=[]:
        x=h.heappop(q)
        for p in hen[x]:
            if distance[p]==-1:
                h.heappush(sub,p)
                distance[p]=distance[x]+1
        #何らかの操作
        #ここでsubキューに次に使うものを格納
        if q==[]:
            q=sub
            sub=[]
            h.heapify(sub)
            break

if distance[goal]==-1:
    print(-1)
else:
    print(distance[goal]//3)

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

N,M=map(int,input().split())
hen=[[] for i in range(0,N)]
for i in range(0,M):
    u,v=map(int,input().split())
    hen[u-1].append(v-1)

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

ans=[-1 for i in range(0,N)]
ans[S-1]=0

q=[]
q.append(S-1)
h.heapify(q)
sub=[]
h.heapify(sub)
count=0
flag=1
while flag==1:
    flag=0
    while q!=[]:
        x=h.heappop(q)
        for p in hen[x]:
                h.heappush(sub,p)
        if q==[]:
            count+=1
            if count%3==0:
                for j in sub:
                    if ans[j]==-1:
                        ans[j]=count//3
                        flag=1
            else:
                flag=1
            q=sub
            sub=[]
            h.heapify(sub)
            break

print(ans[T-1])

Fixed submission:
import heapq as h

N,M=map(int,input().split())
hen={(i,j):[] for i in range(0,N) for j in range(0,3)}
for i in range(0,M):
    u,v=map(int,input().split())
    hen[(u-1,0)].append((v-1,1))
    hen[(u-1,1)].append((v-1,2))
    hen[(u-1,2)].append((v-1,0))

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

start=(S-1,0)
goal=(T-1,0)

distance={(i,j):-1 for i in range(0,N) for j in range(0,3)}

q=[]
q.append(start)
distance[start]=0
h.heapify(q)
sub=[]
h.heapify(sub)
while q!=[]:
    while q!=[]:
        x=h.heappop(q)
        for p in hen[x]:
            if distance[p]==-1:
                h.heappush(sub,p)
                distance[p]=distance[x]+1
        #何らかの操作
        #ここでsubキューに次に使うものを格納
        if q==[]:
            q=sub
            sub=[]
            h.heapify(sub)
            break

if distance[goal]==-1:
    print(-1)
else:
    print(distance[goal]//3)