from collections import deque class DetectDAG: # REQUIRED # from collections import deque def __init__(self, n): self.n = n self.ikeru = [[] for i in range(n)] self.deg = [0] * n def add_edge(self, x, y): self.ikeru[x].append(y) self.deg[y] += 1 def isdag(self): mada = deque() for i in range(self.n): if self.deg[i] == 0: mada.append(i) while mada: i = mada.popleft() for j in self.ikeru[i]: self.deg[j] -= 1 if self.deg[j] == 0: mada.append(j) for i in self.deg: if i > 0: return False return True n, q = map(int,input().split()) x = [0] * q y = [0] * q for i in range(q): a, b = map(int,input().split()) x[i] = a-1 y[i] = b-1 suki = q kirai = -1 while suki - kirai > 1: targ = (suki + kirai) // 2 g = DetectDAG(n) for i in range(targ): g.add_edge(x[i], y[i]) if not g.isdag(): suki = targ else: kirai = targ if suki == q: print(-1) else: print(suki)