from collections import deque, defaultdict n, q = map(int, input().split()) Q = [] for _ in range(q): u, v = map(int, input().split()) u -= 1 v -= 1 Q.append((u, v)) def check_cycle(r): INcnt = defaultdict(int) # INcnt[v]:到着先がvである回数 OUTs = defaultdict(list) # OUTs[u]:出発点がuであるときの到着先リスト for u, v in Q[:r]: INcnt[v] += 1 OUTs[u].append(v) Topo = [] Que = deque([i for i in range(n) if INcnt[i] == 0]) while len(Que) != 0: v = Que.popleft() Topo.append(v) for v2 in OUTs[v]: INcnt[v2] -= 1 if INcnt[v2] == 0: Que.append(v2) return len(Topo) != n left = 0 right = q + 1 while right - left > 1: mid = (left + right) // 2 if check_cycle(mid): right = mid else: left = mid if right > q: print(-1) else: print(right)