import sys from collections import deque input = sys.stdin.buffer.readline N, Q = map(int, input().split()) edges = tuple(tuple(int(x) - 1 for x in input().split()) for _ in range(Q)) deg = [0] * N G = [[] for _ in range(N)] for a, b in edges: deg[b] += 1 G[a].append(b) deg_is_zero = set(i for i, x in enumerate(deg) if not x) d = deque(deg_is_zero) def dfs(d): while d: v = d.popleft() for x in G[v]: deg[x] -= 1 if not deg[x]: d.append(x) deg_is_zero.add(x) dfs(d) if len(deg_is_zero) == N: print(-1) for i in range(Q, 0, -1): b = edges[i - 1][1] deg[b] -= 1 if not deg[b]: deg_is_zero.add(b) dfs(deque([b])) if len(deg_is_zero) == N: print(i)