import sys readline = sys.stdin.readline from heapq import * def topsort(G): Q = [] count = [0] * len(G) for u in range(len(G)): for v in G[u]: count[v] += 1 for u in range(len(G)): if count[u] == 0: heappush(Q, u) anslst = [] while Q: u = heappop(Q) anslst.append(u) temp = [] for v in G[u]: count[v] -= 1 if count[v] == 0: heappush(Q, v) anslst.reverse() return anslst def check(m): G = [[] for i in range(N)] S = set() for i in range(m): v = A[i] * (N + 5) + B[i] if v not in S: G[A[i] - 1].append(B[i] - 1) S.add(v) return len(topsort(G)) == N N, Q = map(int, readline().split()) A, B = [None] * Q, [None] * Q for i in range(Q): A[i], B[i] = map(int, readline().split()) if check(Q): print(-1) exit() yes = 0 no = Q while no - yes != 1: mid = (yes + no)//2 if check(mid): yes = mid else: no = mid print(no)