Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(100000)

def hersCode(L, n):
  #if L[-1] < n:
  #  return len(L)
  ok = 0
  ng = len(L)
  while abs(ok - ng) > 1:
    mid = abs(ok + ng) // 2
    if L[mid] <= n:
      ok = mid
    else:
      ng = mid
  return ok

def answer(T):
  print(len(T))
  for i in T:
    print(i)

N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(M)]

L = {}
R = {}
for i in range(M):
  a, b = AB[i]
  if a in L:
    L[a].append(b)
  else:
    L[a] = [b]
  if b in R:
    R[b].append(a)
  else:
    R[b] = [a]

def nasu(g, n, r, P):
  if n in R:
    for i in R[n]:
      if i in P and i != r:
        return None
  if n not in L:
    return None
  k = L[n]
  for i in k:
    if i in P and i != g:
      return None
  if g in k:
    return P
  for i in k:
    if i < g:
      continue
    if i in P:
      continue
    T = nasu(g, i, n, P + [i])
    if T:
      return T
  return None

  

for i in range(1, N):
  T = nasu(i, i, 0, [i])
  if T:
    answer(T)
    exit()
else:
  print(-1)




Fixed submission:
import sys
sys.setrecursionlimit(100000)

N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(M)]

R = {}
for a, b in AB:
  if a in R:
    R[a].append(b)
  else:
    R[a] = [b]

def nasu(x, e):
  D[x] = 1
  if x not in R:
    return []
  for i in R[x]:
    if i == e:
      return [i]
    if D[i] == 0:
      t = nasu(i, e)
      if t != []:
        return [i] + t
  return []

for i in range(1, N + 1):
  D = [0] * (N + 1)
  L = nasu(i, i)
  if L != []:
    break
else:
  print(-1)
  exit()

D = [0] * (N + 1)
for i in L:
  D[i] = 1

LEN = len(L)
L = L + L

i = 0
while i < LEN:
  for j in range(i + LEN - 1, i, -1):
    if D[L[j]] != 0 and L[j] in R[L[i]]:
      for k in range(j - 1, i, -1):
        D[L[k]] = 0
      i = j
      break

A = []
for i in range(LEN):
  if D[L[i]] == 1:
    A.append(L[i])

print(len(A))
for i in A:
  print(i)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(100000)

def answer(T):
  print(len(T))
  for i in T:
    print(i)

N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(M)]

L = {}
R = {}
for i in range(M):
  a, b = AB[i]
  if a in L:
    L[a].append(b)
  else:
    L[a] = [b]
  if b in R:
    R[b].append(a)
  else:
    R[b] = [a]

def nasu(g, n, r, P):
  if n in R:
    for i in R[n]:
      if i in P and i != r:
        return None
  if n not in L:
    return None
  k = L[n]
  for i in k:
    if i in P and i != g:
      return None
  if g in k:
    return P
  for i in k:
    if i < g:
      continue
    if i in P:
      continue
    T = nasu(g, i, n, P + [i])
    if T:
      return T
  return None

  

for i in range(1, N):
  T = nasu(i, i, 0, [i])
  if T:
    answer(T)
    exit()
else:
  print(-1)




Fixed submission:
import sys
sys.setrecursionlimit(100000)

N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(M)]

R = {}
for a, b in AB:
  if a in R:
    R[a].append(b)
  else:
    R[a] = [b]

def nasu(x, e):
  D[x] = 1
  if x not in R:
    return []
  for i in R[x]:
    if i == e:
      return [i]
    if D[i] == 0:
      t = nasu(i, e)
      if t != []:
        return [i] + t
  return []

for i in range(1, N + 1):
  D = [0] * (N + 1)
  L = nasu(i, i)
  if L != []:
    break
else:
  print(-1)
  exit()

D = [0] * (N + 1)
for i in L:
  D[i] = 1

LEN = len(L)
L = L + L

i = 0
while i < LEN:
  for j in range(i + LEN - 1, i, -1):
    if D[L[j]] != 0 and L[j] in R[L[i]]:
      for k in range(j - 1, i, -1):
        D[L[k]] = 0
      i = j
      break

A = []
for i in range(LEN):
  if D[L[i]] == 1:
    A.append(L[i])

print(len(A))
for i in A:
  print(i)