## https://yukicoder.me/problems/no/1865 from collections import deque def solve(N, next_nodes, border_q): queue = deque() passed = [-1] * N p_id = 0 for s_i in range(N): if passed[s_i] == -1: queue.append(s_i) passed[s_i] = p_id while len(queue) > 0: v = queue.popleft() for w, q in next_nodes[v].items(): if q > border_q: continue if w == s_i: return True if passed[w] == -1: passed[w] = p_id queue.append(w) p_id += 1 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()) a -= 1 b -= 1 if b not in next_nodes[a]: next_nodes[a][b] = i # 全て繋げた状態で ans = solve(N, next_nodes, Q - 1) if not ans: print(-1) return low = 0 high = Q - 1 while high - low > 1: mid = (high + low) // 2 if solve(N, next_nodes, mid): high = mid else: low = mid if solve(N, next_nodes, low): print(low + 1) else: print(high + 1) if __name__ == "__main__": main()