#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; int N, Q; int A[100010]; int B[100010]; map> E; void update_edges(int x) { for (int i = 0; i < Q; ++i) { int a = A[i]; int b = B[i]; if (i < x) { E[a][b] = true; } else { E[a][b] = false; } } } bool dfs(int v, vector &visited) { visited[v] = 1; for (auto &[u, s] : E[v]) { if (not s) continue; if (visited[u] == 1) return true; if (visited[u] == 0 && dfs(u, visited)) return true; } visited[v] = 2; return false; } bool has_cycle() { vector visited(N + 1, 0); for (int v = 1; v <= N; ++v) { if (visited[v] == 0 && dfs(v, visited)) return true; } return false; } int main() { cin >> N >> Q; for (int i = 0; i < Q; ++i) { cin >> A[i] >> B[i]; } update_edges(Q); if (not has_cycle()) { cout << -1 << endl; return 0; } int ok = Q; int ng = 0; while (abs(ok - ng) >= 2) { int x = (ok + ng) / 2; update_edges(x); if (has_cycle()) { ok = x; } else { ng = x; } } cout << ok << endl; return 0; }