Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
from collections import defaultdict
import copy

n, m = map(int, sys.stdin.readline().split())
adj = [[] for _ in range(n+1)]
for _ in range(m):
    a, b = map(int, sys.stdin.readline().split())
    adj[a].append(b)


def searchRoute(i):
    global adj
    nexts = deque()
    route = [i]
    visited = set()
    result = []
    for x in adj[i]:
        if x < i:
            continue
        nroute = copy.copy(route)
        nvisited = copy.copy(visited)
        nexts.append((x, nroute, nvisited))
    while len(nexts) > 0:
        error = False
        found = False
        x, xroute, xvisited = nexts.popleft()
        xroute.append(x)
        xvisited.add(x)
        for y in adj[x]:
            if y < i:
                continue
            if y == i:
                found = True
                break
            if y in xvisited:
                break
            yroute = copy.copy(xroute)
            yvisited = copy.copy(xvisited)
            nexts.append((y, yroute, yvisited))
        if found:
            result = xroute
            break
    return result

heiro = []
for i in range(1, n+1):
    res = searchRoute(i)
    if len(res) > 0:
        heiro = res
        break

if len(heiro) == 0:
    print(-1)
    exit()
while True:
    visited = set()
    bp = [-1, -1]
    rt = defaultdict(int)
    for i in range(len(heiro)):
        visited.add(heiro[i])
        rt[heiro[i]] = i
        for j in adj[heiro[i]]:
            if j in visited and not (i == len(heiro) - 1 and j == heiro[0]):
                bp = [i, rt[j]]
                break
        if bp[0] != -1:
            break
    if bp[0] == -1:
        print(len(heiro))
        for i in heiro:
            print(i)
        exit()
    heiro = heiro[bp[1]:bp[0]+1]



Fixed submission:
import sys
from collections import deque
from collections import defaultdict
import copy

n, m = map(int, sys.stdin.readline().split())
adj = [[] for _ in range(n+1)]
radj = [[] for _ in range(n+1)]
for _ in range(m):
    a, b = map(int, sys.stdin.readline().split())
    adj[a].append(b)
    radj[b]


def searchRoute(i):
    global adj
    nexts = deque()
    route = [i]
    visited = set()
    result = []
    for x in adj[i]:
        if x < i:
            continue
        nroute = copy.copy(route)
        nexts.append((x, nroute))
    while len(nexts) > 0:
        error = False
        found = False
        x, xroute = nexts.popleft()
        xroute.append(x)
        visited.add(x)
        for y in adj[x]:
            if y < i:
                continue
            if y == i:
                found = True
                break
            if y in visited:
                break
            yroute = copy.copy(xroute)
            nexts.append((y, yroute))
        if found:
            result = xroute
            break
    return result

heiro = []
for i in range(1, n+1):
    res = searchRoute(i)
    if len(res) > 0:
        heiro = res
        break

if len(heiro) == 0:
    print(-1)
    exit()
while True:
    visited = set()
    bp = [-1, -1]
    rt = defaultdict(int)
    for i in range(len(heiro)):
        visited.add(heiro[i])
        rt[heiro[i]] = i
        for j in adj[heiro[i]]:
            if j in visited and not (i == len(heiro) - 1 and j == heiro[0]):
                bp = [i, rt[j]]
                break
        if bp[0] != -1:
            break
    if bp[0] == -1:
        print(len(heiro))
        for i in heiro:
            print(i)
        exit()
    heiro = heiro[bp[1]:bp[0]+1]