Buggy python submission (verdict: Time Limit Exceeded):
import math
from collections import deque

N, M = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(M)]
g = [[] for _ in range(N)]
for a, b in AB:
    g[a-1].append(b-1)

q = deque([(i, {i}) for i in range(N)])
min_loop = (math.inf, [])
while len(q) > 0:
    node, path = q.pop()
    for to_node in g[node]:
        if to_node in path:
            if min_loop[0] > len(path):
                min_loop = (len(path), path)
        else:
            q.append((to_node, path | {to_node}))
if min_loop[0] == math.inf:
    print(-1)
else:
    print(min_loop[0])
    for node in min_loop[1]:
        print(node+1)


Fixed submission:
import math
import sys
from collections import deque, defaultdict

N, M = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(M)]
g = [set() for _ in range(N)]
for a, b in AB:
    g[a - 1].add(b - 1)


def find_loop(s, subnode):
    q = deque([(s, {s})])
    visited = defaultdict(lambda: math.inf)
    while len(q) > 0:
        node, p = q.popleft()
        for to_node in g[node] & subnode:
            if to_node == s:
                return p
            if visited[to_node] > len(p):
                visited[to_node] = len(p)
                q.append((to_node, p | {to_node}))
    return -1


V = set(range(N))
for i in range(N):
    subnode = find_loop(i, V)
    if subnode != -1:
        break
if subnode == -1:
    print(-1)
    sys.exit()


while True:
    # 条件判定
    flg = True
    for i in subnode:
        if len(g[i] & subnode) != 1:
            flg = False
            s = i
            break
    if flg:
        break
    subnode = find_loop(s, subnode)

print(len(subnode))
for node in subnode:
    print(node+1)

Buggy python submission (verdict: Time Limit Exceeded):
import math
from collections import deque

N, M = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(M)]
g = [[] for _ in range(N)]
for a, b in AB:
    g[a-1].append(b-1)

q = deque([(i, [i]) for i in range(N)])
min_loop = (math.inf, [])
while len(q) > 0:
    node, path = q.pop()
    for to_node in g[node]:
        if to_node in path:
            if min_loop[0] > len(path):
                min_loop = (len(path), path)
        else:
            q.append((to_node, path + [to_node]))
if min_loop[0] == math.inf:
    print(-1)
else:
    print(min_loop[0])
    for node in min_loop[1]:
        print(node+1)


Fixed submission:
import math
import sys
from collections import deque, defaultdict

N, M = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(M)]
g = [set() for _ in range(N)]
for a, b in AB:
    g[a - 1].add(b - 1)


def find_loop(s, subnode):
    q = deque([(s, {s})])
    visited = defaultdict(lambda: math.inf)
    while len(q) > 0:
        node, p = q.popleft()
        for to_node in g[node] & subnode:
            if to_node == s:
                return p
            if visited[to_node] > len(p):
                visited[to_node] = len(p)
                q.append((to_node, p | {to_node}))
    return -1


V = set(range(N))
for i in range(N):
    subnode = find_loop(i, V)
    if subnode != -1:
        break
if subnode == -1:
    print(-1)
    sys.exit()


while True:
    # 条件判定
    flg = True
    for i in subnode:
        if len(g[i] & subnode) != 1:
            flg = False
            s = i
            break
    if flg:
        break
    subnode = find_loop(s, subnode)

print(len(subnode))
for node in subnode:
    print(node+1)

Buggy python submission (verdict: Runtime Error):
import math

N, M = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(M)]
d = [[math.inf]*N for _ in range(N)]
for a, b in AB:
    d[a-1][b-1] = 1


def warshall_floyd(d):
    for k in range(len(d)):
        for i in range(len(d)):
            for j in range(len(d)):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])


warshall_floyd(d)
min_d = min([d[i][i] for i in range(N)])
if min_d == math.inf:
    print(-1)
else:
    ans = []
    for i in range(N):
        if d[i][i] == min_d:
            ans.append(i + 1)
    if len(ans) != min_d:
        raise ValueError('同じループが二つ')
    print(min_d)
    for i in ans:
        print(i)


Fixed submission:
import math
import sys
from collections import deque, defaultdict

N, M = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(M)]
g = [set() for _ in range(N)]
for a, b in AB:
    g[a - 1].add(b - 1)


def find_loop(s, subnode):
    q = deque([(s, {s})])
    visited = defaultdict(lambda: math.inf)
    while len(q) > 0:
        node, p = q.popleft()
        for to_node in g[node] & subnode:
            if to_node == s:
                return p
            if visited[to_node] > len(p):
                visited[to_node] = len(p)
                q.append((to_node, p | {to_node}))
    return -1


V = set(range(N))
for i in range(N):
    subnode = find_loop(i, V)
    if subnode != -1:
        break
if subnode == -1:
    print(-1)
    sys.exit()


while True:
    # 条件判定
    flg = True
    for i in subnode:
        if len(g[i] & subnode) != 1:
            flg = False
            s = i
            break
    if flg:
        break
    subnode = find_loop(s, subnode)

print(len(subnode))
for node in subnode:
    print(node+1)