Buggy python submission (verdict: Runtime Error):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


def topological_sort(graph):
    """
    :param list of (list of int) graph:
    :return:
    """
    # 入次数
    ins = [0] * len(graph)
    for vs in graph:
        for v in vs:
            ins[v] += 1

    # 入次数がゼロのやつ
    zeros = []
    for v, cnt in enumerate(ins):
        if cnt == 0:
            zeros.append(v)

    # 入次数がゼロのやつから順に追加してく
    ret = []
    while zeros:
        v = zeros.pop()
        ret.append(v)
        for u in graph[v]:
            ins[u] -= 1
            if ins[u] == 0:
                zeros.append(u)

    if len(ret) == len(graph):
        # トポロジカルソートできた
        return []

    ret = []
    for i, c in enumerate(ins):
        if c > 0:
            ret.append(i)
    return ret


N, M = list(map(int, sys.stdin.readline().split()))
AB = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]
graph = [[] for _ in range(N + 1)]

for a, b in AB:
    graph[a].append(b)

cycle = topological_sort(graph)
if not cycle:
    print(-1)
    exit()

candidate = [False] * (N + 1)
for v in cycle:
    candidate[v] = True


def dfs(v, seen, hist):
    cc = []
    for u in graph[v]:
        if not candidate[u]:
            continue
        if seen[u]:
            cc.append(u)
    if cc:
        return hist, cc

    for u in graph[v]:
        if not candidate[u]:
            continue
        seen[u] = True
        hist.append(u)
        ret, lasts = dfs(u, seen, hist)
        seen[u] = False
        if ret:
            return ret, lasts
        hist.pop()
    return False, []


stack = [cycle[0]]
ng = [False] * (N + 1)
while stack:
    v = stack.pop()
    if ng[v]:
        continue
    seen = [False] * (N + 1)
    seen[v] = True
    ret, lasts = dfs(v, seen, [v])
    if lasts == [v]:
        print(len(ret))
        print(*ret, sep='\n')
        exit()
    ng[v] = True
    for v in lasts:
        if not ng[v]:
            stack.append(v)

assert False


Fixed submission:
import os
import sys
from collections import deque

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = list(map(int, sys.stdin.readline().split()))
AB = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]
graph = [[] for _ in range(N + 1)]

for a, b in AB:
    graph[a].append(b)


def cycle_size(from_v):
    """

    :param from_v:
    :return:
    """
    parents = [None] * len(graph)
    seen = [False] * len(graph)
    que = deque([(from_v, 0)])
    while que:
        v, d = que.pop()
        for u in graph[v]:
            if seen[u]:
                continue
            seen[u] = True
            que.appendleft((u, d + 1))
            parents[u] = v
            if u == from_v:
                return d + 1, parents
    return INF, parents


min_v = None
min_size = INF
min_parents = None
for v in range(1, N + 1):
    size, parents = cycle_size(v)
    if size < min_size:
        min_v = v
        min_size = size
        min_parents = parents

if min_size < INF:
    print(min_size)
    print(min_v)
    v = min_parents[min_v]
    while v != min_v:
        print(v)
        v = min_parents[v]
else:
    print(-1)