## https://yukicoder.me/problems/no/1865 from collections import deque MAX_INT = 10 ** 18 def exists_cycle(N, next_nodes, border): passed = [False] * N passed2 = [False] * N for s in range(N): if not passed[s]: passed[s] = True stack = deque() stack.append((s, 0)) while len(stack) > 0: v, index = stack.pop() passed2[v] = True while index < len(next_nodes[v]): w, i = next_nodes[v][index] if i > border: index += 1 continue if passed[w]: if passed2[w]: return True else: index += 1 continue stack.append((v, index + 1)) stack.append((w, 0)) passed[w] = True break if index == len(next_nodes[v]): passed2[v] = False return False def main(): N, Q = map(int, input().split()) next_nodes = [[] for _ in range(N)] for i in range(Q): A, B = map(int ,input().split()) next_nodes[A - 1].append((B - 1, i)) # そもそも閉路が存在するか? if not exists_cycle(N, next_nodes, Q - 1): print(-1) return low = 0 high = Q - 1 while high - low > 1: mid = (high + low ) //2 if exists_cycle(N, next_nodes, mid): high = mid else: low = mid if exists_cycle(N, next_nodes, low): print(low + 1) else: print(high + 1) if __name__ == "__main__": main()