Buggy python submission (verdict: Time Limit Exceeded):
N,M = list(map(int,input().split()))

e_list = [[] for i in range(N)]
es = []
for i in range(M):
    a,b = list(map(int,input().split()))
    a,b = a-1,b-1
    es.append((a,b))
    e_list[a].append((b,1))

import heapq
flag=False
min_d = 10**27
for vg,vi in es:
    #dijkstra
    #you need to prepare e_list = [[orient,dist]]
    
    min_d_list = [10**27 for i in range(N)]
    min_d_list[vi] = 0
    prev_list = [-1 for i in range(N)]
    q = []
    for i in range(N):
        heapq.heappush(q,[min_d_list[i],i])
    
    while len(q)>0:
        d,v = heapq.heappop(q)
        for e in e_list[v]:
            v1,d1=e
            if v!=vg or v1!=vi:
                if min_d_list[v1]>d+d1:
                    min_d_list[v1]=d+d1
                    prev_list[v1]=v
                    heapq.heappush(q,[d+d1,v1])
    if min_d_list[vg]<min_d:
        flag=True
        min_vi = vi
        min_vg = vg
        min_d = min_d_list[vg]
        min_prev = prev_list
            
#print(min_d_list)
if flag:
    ans = []
    v = min_vg
    while v!=min_vi:
        ans.append(v+1)
        v=min_prev[v]
    ans.append(min_vi+1)
    ans = ans[::-1]
    
    print(len(ans))
    for a in ans:
        print(a)
else:
    print(-1)

Fixed submission:
N,M = list(map(int,input().split()))

e_list = [[] for i in range(N)]
es = []
for i in range(M):
    a,b = list(map(int,input().split()))
    a,b = a-1,b-1
    es.append((a,b))
    e_list[a].append(b)

from collections import deque
flag=False
min_d = 10**27
for vg,vi in es:
    Q = deque([vi])
    checked_list = [False for i in range(N)]
    checked_list[vi]=True
    min_path_list = [10**27 for i in range(N)] #問題によりここを変える
    min_path_list[vi] = 0
    prev_list = [-1]*N
    while len(Q)>0:
        v = Q.pop()
        for v1 in e_list[v]:
            if v!=vg or v1!=vi:
                if not checked_list[v1]:
                    checked_list[v1]=True
                    Q.appendleft(v1)
                    prev_list[v1]=v
                    min_path_list[v1]=min(min_path_list[v1],min_path_list[v]+1) #問題によりここを変える
    if min_path_list[vg]<min_d:
        flag=True
        min_vi = vi
        min_vg = vg
        min_d = min_path_list[vg]
        min_prev = prev_list
            
#print(min_d_list)
if flag:
    ans = []
    v = min_vg
    while v!=min_vi:
        ans.append(v+1)
        v=min_prev[v]
    ans.append(min_vi+1)
    ans = ans[::-1]
    
    print(len(ans))
    for a in ans:
        print(a)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = list(map(int,input().split()))

e_list = [[] for i in range(N)]

for i in range(M):
    a,b = list(map(int,input().split()))
    a,b = a-1,b-1
    e_list[a].append((b,1))

import heapq
flag=False
min_d = 10**27
for vg in range(N):
    for vi,_ in e_list[vg]:
        #dijkstra
        #you need to prepare e_list = [[orient,dist]]
        
        min_d_list = [10**27 for i in range(N)]
        min_d_list[vi] = 0
        prev_list = [-1 for i in range(N)]
        q = []
        for i in range(N):
            heapq.heappush(q,[min_d_list[i],i])
        
        while len(q)>0:
            d,v = heapq.heappop(q)
            for e in e_list[v]:
                v1,d1=e
                if v!=vg or v1!=vi:
                    if min_d_list[v1]>d+d1:
                        min_d_list[v1]=d+d1
                        prev_list[v1]=v
                        heapq.heappush(q,[d+d1,v1])
        if min_d_list[vg]<min_d:
            flag=True
            min_vi = vi
            min_vg = vg
            min_d = min_d_list[vg]
            min_prev = prev_list
            
#print(min_d_list)
if flag:
    ans = []
    v = min_vg
    while v!=min_vi:
        ans.append(v+1)
        v=min_prev[v]
    ans.append(min_vi+1)
    ans = ans[::-1]
    
    print(len(ans))
    for a in ans:
        print(a)
else:
    print(-1)

Fixed submission:
N,M = list(map(int,input().split()))

e_list = [[] for i in range(N)]
es = []
for i in range(M):
    a,b = list(map(int,input().split()))
    a,b = a-1,b-1
    es.append((a,b))
    e_list[a].append(b)

from collections import deque
flag=False
min_d = 10**27
for vg,vi in es:
    Q = deque([vi])
    checked_list = [False for i in range(N)]
    checked_list[vi]=True
    min_path_list = [10**27 for i in range(N)] #問題によりここを変える
    min_path_list[vi] = 0
    prev_list = [-1]*N
    while len(Q)>0:
        v = Q.pop()
        for v1 in e_list[v]:
            if v!=vg or v1!=vi:
                if not checked_list[v1]:
                    checked_list[v1]=True
                    Q.appendleft(v1)
                    prev_list[v1]=v
                    min_path_list[v1]=min(min_path_list[v1],min_path_list[v]+1) #問題によりここを変える
    if min_path_list[vg]<min_d:
        flag=True
        min_vi = vi
        min_vg = vg
        min_d = min_path_list[vg]
        min_prev = prev_list
            
#print(min_d_list)
if flag:
    ans = []
    v = min_vg
    while v!=min_vi:
        ans.append(v+1)
        v=min_prev[v]
    ans.append(min_vi+1)
    ans = ans[::-1]
    
    print(len(ans))
    for a in ans:
        print(a)
else:
    print(-1)