N,Q = map(int,input().split()) G = [[] for _ in range(N+1)] Gnum = [0] * (N + 1) edge = [] for _ in range(Q): a,b = map(int,input().split()) G[a].append(b) Gnum[b] += 1 edge.append((a,b)) count = 0 top = [] stack = [] for i in range(1,N+1): if Gnum[i] == 0: stack.append(i) count += 1 while stack: now = stack.pop() for v in G[now]: Gnum[v] -= 1 if Gnum[v] == 0: count += 1 stack.append(v) import sys if count == N: print(-1) exit() for i,u in enumerate(reversed(edge)): a,b = u Gnum[b] -= 1 if Gnum[b] == 0: count += 1 stack.append(b) while stack: now = stack.pop() for v in G[now]: Gnum[v] -= 1 if Gnum[v] == 0: count += 1 stack.append(v) if count == N: print(Q - i) exit()