import sys from collections import deque input = lambda: sys.stdin.readline().rstrip() def topological_sort(G: list) -> list: "Return topological_sort. / O(|V|+|E|)" # 0-indexed n = len(G) in_cnt = [0] * n outs = [[] for _ in range(n)] for v in range(n): for x in G[v]: in_cnt[x] += 1 outs[v].append(x) res = [] todo = deque([i for i in range(n) if in_cnt[i] == 0]) while todo: v = todo.popleft() res.append(v) for x in outs[v]: in_cnt[x] -= 1 if in_cnt[x] == 0: todo.append(x) return res # ----------------------- # n, q = map(int, input().split()) AB = [list(map(int, input().split())) for _ in range(q)] def isok(mid): G = [[] for _ in range(n)] for i in range(mid): a = AB[i][0] - 1 b = AB[i][1] - 1 G[a].append(b) toposo = topological_sort(G) if len(toposo) != n: return True else: return False ok, ng = q+1, -1 while ok - ng > 1: mid = (ok + ng) // 2 if isok(mid): ok = mid else: ng = mid if ok == q+1: print(-1) else: print(ok)