Buggy python submission (verdict: Runtime Error):
from collections import deque

n,m = map(int,input().split())
G = [list() for _ in range(n+1)]
R = [list() for _ in range(n+1)]
for _ in range(m):
  u,v = map(int,input().split())
  G[u].append(v)
  R[v].append(u)

def find_cycle(visited = [1]):
  v = visited[-1]
  for x in G[v]:
    if x in visited:
      return visited[visited.index(x):]
    res = find_cycle(visited + [x])
    if len(res) > 0:
      return res
  return list()

def trash(i):
  dis = [n]*(n+1)
  dis[i] = 0
  q = deque([i])
  while q:
    v = q.popleft()
    for x in G[v]:
      if x == i: continue
      if dis[x] < dis[v] + 1:
        # print(dis,i,v,x)
        return v
      dis[x] = dis[v] + 1
      q.append(x)
  return False

for i in range(1,n+1):
  cyc = find_cycle([i])
  if cyc: break
  else: print(-1); exit(0)

while True:
  # print(cyc)
  for i in range(1,n+1):
    if i not in cyc:
      for x in G[i]:
        R[x].remove(i)
      G[i] = []
      for x in R[i]:
        G[x].remove(i)
      R[i] = []
     
  for i in cyc:
    if len(G[i]) > 1:
      st = i
      v = trash(st)
      for x in G[v]:
        R[x].remove(v)
      G[v] = []
      for x in R[v]:
        G[x].remove(v)
      R[v] = []
      # print(G)
      # print(R)
      cyc = set(find_cycle([st]))
      break
  else: break
if not cyc: print(-1); exit(0)     
print(len(cyc))
print(*cyc, sep='\n')

Fixed submission:
from collections import deque

n,m = map(int,input().split())
g = [list() for _ in range(n+1)]
en = [0]*(n+1)
for _ in range(m):
  u,v = map(int,input().split())
  g[u].append(v)
  en[v] += 1
q = deque(i for i in range(1,n+1) if en[i] == 0)
done = list()
while q:
  v = q.popleft()
  for x in g[v]:
    en[x] -= 1
    if not en[x]:
      q.append(x)
  done.append(v)
print(-1 if len(done) == n else 0)

Buggy python submission (verdict: Runtime Error):
from collections import deque

n,m = map(int,input().split())
G = [list() for _ in range(n+1)]
R = [list() for _ in range(n+1)]
for _ in range(m):
  u,v = map(int,input().split())
  G[u].append(v)
  R[v].append(u)

def find_cycle(visited = [1]):
  v = visited[-1]
  for x in G[v]:
    if x in visited:
      return visited[visited.index(x):]
    res = find_cycle(visited + [x])
    if len(res) > 0:
      return res
  return list()

def trash(i):
  dis = [n]*(n+1)
  dis[i] = 0
  q = deque([i])
  while q:
    v = q.popleft()
    for x in G[v]:
      if x == i: continue
      if dis[x] < dis[v] + 1:
        # print(dis,i,v,x)
        return v
      dis[x] = dis[v] + 1
      q.append(x)
  return False

for i in range(1,n+1):
  cyc = find_cycle([i])
  if cyc: break
  else: print(-1); exit(0)

while True:
  # print(cyc)
  for i in range(1,n+1):
    if i not in cyc:
      for x in G[i]:
        R[x].remove(i)
      G[i] = []
      for x in R[i]:
        G[x].remove(i)
      R[i] = []
     
  for i in cyc:
    if len(G[i]) > 1:
      st = i
      v = trash(st)
      for x in G[v]:
        R[x].remove(v)
      G[v] = []
      for x in R[v]:
        G[x].remove(v)
      R[v] = []
      # print(G)
      # print(R)
      cyc = set(find_cycle([st]))
      break
  else: break
      
print(len(cyc))
print(*cyc, sep='\n')

Fixed submission:
from collections import deque

n,m = map(int,input().split())
g = [list() for _ in range(n+1)]
en = [0]*(n+1)
for _ in range(m):
  u,v = map(int,input().split())
  g[u].append(v)
  en[v] += 1
q = deque(i for i in range(1,n+1) if en[i] == 0)
done = list()
while q:
  v = q.popleft()
  for x in g[v]:
    en[x] -= 1
    if not en[x]:
      q.append(x)
  done.append(v)
print(-1 if len(done) == n else 0)