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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():    
    mod=10**9+7
    N,M=MI()
    adj=[[]for _ in range(N)]
    
    for i in range(M):
        a,b=MI()
        a-=1
        b-=1
        adj[a].append(b)
    
    import queue
    use=[0]*N

    
    def bfs(a):
        #a=>aの最短距離を求める
        q=queue.Queue()
        q.put((a,-1,0))
        use[a]=1
        for i in range(N):
            use[i]=0
        
        while not(q.empty()):
            v,p,d=q.get()
            
            for nv in adj[v]:
                if nv==a:
                    return d+1
                else:
                    if nv!=p:
                        if use[nv]==0:
                            use[nv]=1
                            q.put((nv,v,d+1))
                            
        return -1
    
    def bfs2(a):
        #aからaまでの
        #経路復元も
        P=[-1]*N
        q=queue.Queue()
        q.put((a,-1,0))
        for i in range(N):
            use[i]=0
        use[a]=1
        flag=0
        
        while not(q.empty()):
            if flag:
                break
            v,p,d=q.get()
            #print(v,p,d)
            P[v]=p

            for nv in adj[v]:
                #print("nv:",nv)
                if nv==a:
                    flag=1
                    break
                if nv!=p:
                    if use[nv]==0:
                        use[nv]=1
                        q.put((nv,v,d+1))
                            
        L=[]
        flag=1
        for i in range(N):
            #print(v)
            L.append(v)
            v=P[v]
            if v==a:
                L.append(a)
                break
        return L
        
                        
                        
        

    
    ans=N+1
    ansi=0

    for i in range(N):#全ての辺をなめる
        temp=bfs(i)#ここはab間の最短距離をbfsで求める
        if temp!=-1:
            if temp<ans:
                ans=temp
                ansi=i

    
    if ans==N+1:
        print(-1)
    else:
        print(ans)
        ans=bfs2(ansi)
        for i in ans:
            print(i+1)
        
        
        

main()


Fixed submission:
n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    edges[a - 1].append(b - 1)

ret = [0] * (n + 1)
for start in range(n):
    parent = [-1] * n
    q = [start]
    while len(q) > 0:
        i = q.pop(0)
        for j in edges[i]:
            if j == start:
                loop = [i]
                while i != start:
                    i = parent[i]
                    loop.append(i)
                if len(ret) > len(loop):
                    ret = loop
                break
            if parent[j] == -1:
                parent[j] = i
                q.append(j)

if len(ret) > n:
    print(-1)
else:
    print(len(ret))
    for i in reversed(ret):
        print(i + 1)

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():    
    mod=10**9+7
    N,M=MI()
    adj=[[]for _ in range(N)]
    A=[0]*M
    B=[0]*M
    
    for i in range(M):
        A[i],B[i]=MI()
        A[i]-=1
        B[i]-=1
        adj[A[i]].append(B[i])
    
    import queue

    
    def bfs(a):
        #a=>aの最短距離を求める
        q=queue.Queue()
        q.put((a,-1,0))
        use=[0]*N
        use[a]=1
        while not(q.empty()):
            v,p,d=q.get()
            
            for nv in adj[v]:
                if nv==a:
                    return d+1
                else:
                    if nv!=p:
                        if use[nv]==0:
                            use[nv]=1
                            q.put((nv,v,d+1))
                            
        return -1
    
    def bfs2(a):
        #aからaまでの
        #経路復元も
        P=[-1]*N
        q=queue.Queue()
        q.put((a,-1,0))
        use=[0]*N
        use[a]=1
        flag=0
        
        while not(q.empty()):
            if flag:
                break
            v,p,d=q.get()
            #print(v,p,d)
            P[v]=p

            for nv in adj[v]:
                #print("nv:",nv)
                if nv==a:
                    flag=1
                    break
                if nv!=p:
                    if use[nv]==0:
                        use[nv]=1
                        q.put((nv,v,d+1))
                            
        L=[]
        flag=1
        for i in range(N):
            #print(v)
            L.append(v)
            v=P[v]
            if v==a:
                L.append(a)
                break
        return L
        
                        
                        
        

    
    ans=N+1
    ansi=0

    for i in range(N):#全ての辺をなめる
        temp=bfs(i)#ここはab間の最短距離をbfsで求める
        if temp!=-1:
            if temp<ans:
                ans=temp
                ansi=i

    
    if ans==N+1:
        print(-1)
    else:
        print(ans)
    
    
        
        ans=bfs2(ansi)
        for i in ans:
            print(i+1)
        
        
        

main()


Fixed submission:
n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    edges[a - 1].append(b - 1)

ret = [0] * (n + 1)
for start in range(n):
    parent = [-1] * n
    q = [start]
    while len(q) > 0:
        i = q.pop(0)
        for j in edges[i]:
            if j == start:
                loop = [i]
                while i != start:
                    i = parent[i]
                    loop.append(i)
                if len(ret) > len(loop):
                    ret = loop
                break
            if parent[j] == -1:
                parent[j] = i
                q.append(j)

if len(ret) > n:
    print(-1)
else:
    print(len(ret))
    for i in reversed(ret):
        print(i + 1)

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():    
    mod=10**9+7
    N,M=MI()
    adj=[[]for _ in range(N)]
    A=[0]*M
    B=[0]*M
    
    for i in range(M):
        A[i],B[i]=MI()
        A[i]-=1
        B[i]-=1
        adj[A[i]].append(B[i])
    
    def loop(n, X, i0,p0):
        # i0が根
        ans=[]
        
        st = [(~i0,p0), (i0,~p0)]

        while st:
            #頂点 i を見る
            
            (i,p) = st.pop()
            #print(i,ans,usedf,st)
            
            #帰り際の処理，ここを入れる場合，
            if i < 0:
                ans.pop()
                continue
            
            #行きがけの処理
            if i >= 0:
                if i==p0:
                    ans.append(p0)
                    break
                
                ans.append(i)
                
            for a in X[i][::-1]:
                if a!=p and a!=~p:
                    st.append((~a,(abs(i))))
                    st.append((a,abs(i)))
                    
        return ans

    ans=[1]*(N+1)
    la=N+1
    for i in range(M):
        temp=loop(N,adj,B[i],A[i])
        if temp!=[]:
            lt=len(temp)
            if lt<la:
                ans=[]
                la=lt
                for i in range(lt):
                    ans.append(temp[i])

    
    if la==(N+1):
        print(-1)
    else:
        print(la)
        for i in ans:
            print(i+1)
        
        

main()


Fixed submission:
n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    edges[a - 1].append(b - 1)

ret = [0] * (n + 1)
for start in range(n):
    parent = [-1] * n
    q = [start]
    while len(q) > 0:
        i = q.pop(0)
        for j in edges[i]:
            if j == start:
                loop = [i]
                while i != start:
                    i = parent[i]
                    loop.append(i)
                if len(ret) > len(loop):
                    ret = loop
                break
            if parent[j] == -1:
                parent[j] = i
                q.append(j)

if len(ret) > n:
    print(-1)
else:
    print(len(ret))
    for i in reversed(ret):
        print(i + 1)