using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray(); static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray(); static void Main() { var c = NList; var (n, q) = (c[0], c[1]); var map = NMap(q); var tree = new List<(int id, int to)>[n]; for (var i = 0; i < tree.Length; ++i) tree[i] = new List<(int id, int to)>(); for (var i = 0; i < map.Length; ++i) { tree[map[i][0]].Add((i, map[i][1])); } if (!HasLoop(tree, q)) { WriteLine(-1); return; } var ok = q; var ng = 0; while (ok - ng > 1) { var center = (ng + ok) / 2; if (HasLoop(tree, center)) ok = center; else ng = center; } WriteLine(ok); } static bool HasLoop(List<(int id, int to)>[] tree, int time) { var refcount = new int[tree.Length]; foreach (var from in tree) { foreach (var edge in from) { if (edge.id < time) ++refcount[edge.to]; } } var q = new Queue(); for (var i = 0; i < refcount.Length; ++i) if (refcount[i] == 0) q.Enqueue(i); while (q.Count > 0) { var cur = q.Dequeue(); foreach (var edge in tree[cur]) { if (edge.id < time) { --refcount[edge.to]; if (refcount[edge.to] == 0) q.Enqueue(edge.to); } } } return refcount.Max() > 0; } }