from collections import deque def cycle_detectable_topological_sort(g, ind): V = len(g) order = [] depth = [-1]*V for i in range(V): if not ind[i]: order.append(i) depth[i] = 0 q = deque(order) while q: v = q.popleft() cur_depth = depth[v] for u in g[v]: ind[u] -= 1 if not ind[u]: depth[u] = max(depth[u], cur_depth+1) q.append(u) order.append(u) if len(order) == V: return (order, depth) else: return (None, None) import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n, q = map(int, input().split()) AB = [] for i in range(q): a, b = map(int, input().split()) a, b = a-1, b-1 AB.append((a, b)) g = [[] for i in range(n)] ind = [0]*n for a, b in AB: g[a].append(b) ind[b] += 1 order, _ = cycle_detectable_topological_sort(g, ind) if order is not None: print(-1) exit() def is_ok(x): g = [[] for i in range(n)] ind = [0]*n for i in range(x+1): a, b = AB[i] g[a].append(b) ind[b] += 1 order, _ = cycle_detectable_topological_sort(g, ind) return order is None ng = -1 ok = q-1 while ng+1