Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    A=[[INF]*n for _ in range(n)]
    for _ in range(m):
        a,b=map(int,input().split())
        a-=1; b-=1;
        A[a][b]=1
    next=[list(range(n)) for _ in range(n)]
    from itertools import product
    for k,i,j in product(range(n),repeat=3):
        if(A[i][j]>A[i][k]+A[k][j]):
            A[i][j]=A[i][k]+A[k][j]
            next[i][j]=next[i][k]
    length=n+1
    v=-1
    for i in range(n):
        if(length>A[i][i]):
            length=A[i][i]
            v=i
    if(v==-1):
        print(-1)
        return
    print(length)
    print(v+1)
    u=next[v][v]
    while(u!=v):
        print(u+1)
        u=next[u][v]
resolve()

Fixed submission:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    E=[[] for _ in range(n)]
    for _ in range(m):
        a,b=map(lambda x:int(x)-1,input().split())
        E[a].append(b)

    # bfs
    def bfs(sv):
        prev=[-1]*n
        flag=False
        from collections import deque
        Q=deque([sv])
        while(Q):
            v=Q.popleft()
            for nv in E[v]:
                if(nv==sv):
                    flag=True
                    break
                elif(prev[nv]==-1):
                    prev[nv]=v
                    Q.append(nv)
            if(flag): break
        if(not flag):
            return [0]*(n+1)
        else:
            loop=[]
            while(v!=-1):
                loop.append(v)
                v=prev[v]
            loop.reverse()
            return loop

    # calculate
    ans=[0]*(n+1)
    for i in range(n):
        loop=bfs(i)
        if(len(loop)<len(ans)):
            ans=loop
    if(len(ans)==n+1):
        print(-1)
    else:
        print(len(ans))
        for v in ans: print(v+1)
resolve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    A=[[INF]*n for _ in range(n)]
    for _ in range(m):
        a,b=map(lambda x:int(x)-1,input().split())
        A[a][b]=1
    # Warshall-Floyd
    B=[a[:] for a in A] # deep copy
    from itertools import product
    for k,i,j in product(range(n),repeat=3):
        B[i][j]=min(B[i][j],B[i][k]+B[k][j])
    # output
    length=INF
    index=-1
    for i in range(n):
        if(length>B[i][i]):
            length=B[i][i]
            index=i
    if(length==INF):
        print(-1)
        return
    # path restroration
    u=v=index
    path=[]
    while(B[u][v]!=A[u][v]):
        for w in range(n):
            if(B[u][v]==A[u][w]+B[w][v]):
                u=w
                path.append(w)
                break
    path.append(v)
    print(len(path))
    for v in path: print(v+1)
resolve()

Fixed submission:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    E=[[] for _ in range(n)]
    for _ in range(m):
        a,b=map(lambda x:int(x)-1,input().split())
        E[a].append(b)

    # bfs
    def bfs(sv):
        prev=[-1]*n
        flag=False
        from collections import deque
        Q=deque([sv])
        while(Q):
            v=Q.popleft()
            for nv in E[v]:
                if(nv==sv):
                    flag=True
                    break
                elif(prev[nv]==-1):
                    prev[nv]=v
                    Q.append(nv)
            if(flag): break
        if(not flag):
            return [0]*(n+1)
        else:
            loop=[]
            while(v!=-1):
                loop.append(v)
                v=prev[v]
            loop.reverse()
            return loop

    # calculate
    ans=[0]*(n+1)
    for i in range(n):
        loop=bfs(i)
        if(len(loop)<len(ans)):
            ans=loop
    if(len(ans)==n+1):
        print(-1)
    else:
        print(len(ans))
        for v in ans: print(v+1)
resolve()