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 i, (a, b) in enumerate(edges): deg[b] += 1 G[a].append((i, b)) deg_is_zero = set(i for i, x in enumerate(deg) if not x) d = deque(deg_is_zero) nonvisited_edge = [True] * Q def dfs(d): while d: v = d.popleft() for i, x in G[v]: nonvisited_edge[i] = False 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): if nonvisited_edge[i - 1]: b = edges[i - 1][1] deg[b] -= 1 nonvisited_edge[i - 1] = False if not deg[b]: deg_is_zero.add(b) dfs(deque([b])) if len(deg_is_zero) == N: print(i)