from sys import stdin n, q, *indata = map(int, stdin.read().split()) offset = 0 left = 0 right = q+1 while right - left >= 2: mid = (right + left) // 2 g = [[] for i in range(n)] for i in range(mid): s, t = indata[i*2]-1,indata[i*2+1]-1 g[s].append(t) check = [0 for j in range(n)] cyclecheck = False for j in range(n): if check[j] == 0: que = [(j,0)] while que: now, inout = que.pop() if inout == 0: if check[now] == 0: que.append((now,1)) check[now] = 1 for k in g[now]: if check[k] == 1: cyclecheck = True break elif check[k] == 0: que.append((k,0)) if cyclecheck: break else: check[now] = 2 if cyclecheck: break if cyclecheck: right = mid else: left = mid print("{}".format(right))