#include #include #include #include using namespace std; using Graph = vector>; // 探索 vector seen, finished; bool ans = false; void dfs(const Graph &G, int v, int p) { seen[v] = true; for (auto nv : G[v]) { if (nv == p) continue; // 逆流を禁止する // 完全終了した頂点はスルー if (finished[nv]) continue; // サイクルを検出 if (seen[nv] && !finished[nv]) { ans = true; return; } // 再帰的に探索 dfs(G, nv, v); // サイクル検出したならば真っ直ぐに抜けていく if (ans) return; } finished[v] = true; } int main() { // 頂点数 (サイクルを一つ含むグラフなので辺数は N で確定) int N; cin >> N; int Q; cin >> Q; // グラフ入力受取 Graph G(N); for (int i = 0; i < Q; ++i) { int a, b; cin >> a >> b; --a, --b; // 頂点番号が 1-indexed で与えられるので 0-indexed にする G[a].push_back(b); seen.assign(N, false), finished.assign(N, false); dfs(G, 0, -1); if(ans) { cout << i+1 << endl; return 0; } } return 0; }