Buggy python submission (verdict: Time Limit Exceeded):
def dijkstra_back(s,t,n,w,cost):
    #sからtへの最短経路の経路復元
    prev = [s] * n #最短経路の直前の頂点
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0

    while True:
        v = -1
        for i in range(n):
            if (not used[i]) and (v == -1):
               v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
               break
        used[v] = True

        for i in range(n):
            if d[i] > d[v] + cost[v][i]:
                d[i] = d[v] + cost[v][i]
                prev[i] = v

    path = [t]
    while prev[t] != s:
        path.append(prev[t])
        prev[t] = prev[prev[t]]
    path.append(s)
    path = path[::-1]
    return path



N,M=map(int,input().split())
fr={i:set([]) for i in range(1,N+1)}
to={i:set([]) for i in range(1,N+1)}
for i in range(0,M):
    a,b=map(int,input().split())
    fr[b].add(a)
    to[a].add(b)

delete=set([])
flag=1
while flag==1:
    flag=0
    for i in range(1,N+1):
        if (not i in delete) and (delete>=fr[i] or delete>=to[i]):
            delete.add(i)
            flag=1

node=[i for i in range(1,N+1)]
save=set(node)-delete
if save:
    hen=[]
    for i in save:
        fr[i]=fr[i]&save
        to[i]=to[i]&save

    for i in save:
        for j in fr[i]:
            hen.append((j,i))

    save=list(save)

    cost=[[float('inf') for i in range(0,N+1)] for i in range(0,N+1)]
    for i in range(0,len(hen)):
        a,b=hen[i]
        cost[a][b]=1

    ansc=10**10
    ans=[]
    for i in range(len(save)):
        for j in fr[save[i]]:
            test=dijkstra_back(save[i],j,N+1,len(hen),cost)
            if len(test)<ansc:
                ansc=len(test)
                ans=test

    print(ansc)
    for i in range(0,ansc):
        print(ans[i])

else:
    print(-1)

Fixed submission:
def dijkstra_back(s,t,n,w,cost):
    import heapq
    #sからtへの最短経路の経路復元
    prev = [s] * n #最短経路の直前の頂点
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    q=[]
    heapq.heapify(q)
    heapq.heappush(q,(0,s))
    while q:
        c,v=heapq.heappop(q)
        if d[v]<c:
            continue

        for i in range(len(to[v])):
            k=to[v][i]
            if d[k]>d[v]+cost[v][k]:
                d[k]=d[v]+cost[v][k]
                heapq.heappush(q,(d[k],k))
                prev[k]=v

    path = [t]
    while prev[t] != s:
        path.append(prev[t])
        prev[t] = prev[prev[t]]
    path.append(s)
    path = path[::-1]
    return path



N,M=map(int,input().split())
fr={i:set([]) for i in range(1,N+1)}
to={i:set([]) for i in range(1,N+1)}
for i in range(0,M):
    a,b=map(int,input().split())
    fr[b].add(a)
    to[a].add(b)

delete=set([])
flag=1
while flag==1:
    flag=0
    for i in range(1,N+1):
        if (not i in delete) and (delete>=fr[i] or delete>=to[i]):
            delete.add(i)
            flag=1

node=[i for i in range(1,N+1)]
save=set(node)-delete
if save:
    hen=[]
    for i in save:
        fr[i]=fr[i]&save
        to[i]=to[i]&save
        fr[i]=list(fr[i])
        to[i]=list(to[i])

    for i in save:
        for j in fr[i]:
            hen.append((j,i))

    save=list(save)

    cost=[[float('inf') for i in range(0,N+1)] for i in range(0,N+1)]
    for i in range(0,len(hen)):
        a,b=hen[i]
        cost[a][b]=1

    ansc=10**10
    ans=[]
    for i in range(len(save)):
        for j in fr[save[i]]:
            test=dijkstra_back(save[i],j,N+1,len(hen),cost)
            if len(test)<ansc:
                ansc=len(test)
                ans=test

    print(ansc)
    for i in range(0,ansc):
        print(ans[i])

else:
    print(-1)