#include using namespace std; bool dfs(int i, vector& seen, vector >& g) { seen[i] = 1; for (auto& j : g[i]) { if (seen[j] == 1 || (seen[j] == 0 && dfs(j, seen, g))) { return true; } } seen[i] = 2; return false; } int main() { int n, q; cin >> n >> q; vector > edges(q); for (int i = 0; i < q; i++) { int a, b; cin >> a >> b; edges[i] = { a - 1, b - 1 }; } int sq = sqrtl(q); int block_size = (q + sq - 1) / sq; auto check = [&](int i) { vector > g(n); for (int j = 0; j < i; j++) { g[edges[j].first].push_back(edges[j].second); } vector seen(n, 0); bool has_cycle = false; for (int i = 0; i < n; i++) { if (seen[i] == 0 && dfs(i, seen, g)) { has_cycle = true; break; } } return has_cycle; }; int idx = 0; for (int i = 0; i <= sq; i++) { if (check(min(q, i * block_size))) { idx = (i - 1) * block_size; break; } } for (int i = 0; i <= block_size; i++) { if (check(min(q, idx + i))) { cout << idx + i << endl; return 0; } } cout << -1 << endl; }