Buggy python submission (verdict: Runtime Error):
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)

def dfs(index, visited):
    visited[index] = True
    for i in edges[index]:
        if visited.get(i):
            return [i]
        ret = dfs(i, visited)
        if ret:
            return [i] + ret
    visited[index] = False
    return None

def find_local_minimum(cycle):
    n = len(cycle)
    ind = {v: i for (i, v) in zip(range(n), cycle)}
    ret = []
    i = 0
    while True:
        ret.append(i)
        nxt = 0
        for j in [ind[v] for v in edges[cycle[i]] if v in ind]:
            if j < i:
                j += n
            nxt = max(nxt, j)
        if nxt >= n:
            ret = ret[ret.index(nxt % n):]
            break
        i += 1
        while i < nxt:
            del ind[cycle[i]]
            i += 1
    return [cycle[i % n] for i in ret]

cycle = None
visited = {}
for start in range(n):
    if start not in visited:
        cycle = dfs(start, visited)
        if cycle:
            break

if cycle:
    ret = find_local_minimum(cycle)
    print(len(ret))
    for i in ret:
        print(i + 1)
else:
    print(-1)



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: Runtime Error):
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)
parent = [-1] * n
for start in range(n):
    q = [start]
    parent[start] = start
    while len(q) > 0:
        i = q.pop(0)
        for j in edges[i]:
            if parent[j] = j:
                loop = [i]
                while parent[i] != i:
                    i = parent[i]
                    loop.append(i)
                if i == start and 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)




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)