#include using namespace std; #ifdef _RUTHEN #include "debug.hpp" #else #define show(...) true #endif vector topological_sort(vector> &G) { int N = (int)G.size(); vector deg(N, 0); // 入次数をメモ for (int i = 0; i < N; i++) { for (int &v : G[i]) deg[v]++; } // 入次数0の点の集合を作成 queue que; for (int i = 0; i < N; i++) { if (deg[i] == 0) que.push(i); } vector res; while (!que.empty()) { int v = que.front(); que.pop(); res.push_back(v); for (int &nx : G[v]) { // 入次数を減らす deg[nx]--; // 新たな入次数0の点を追加 if (deg[nx] == 0) que.push(nx); } } return res; } using ll = long long; #define rep(i, n) for (int i = 0; i < (n); i++) template using V = vector; int main() { ios::sync_with_stdio(false); cin.tie(0); int N, Q; cin >> N >> Q; V a(Q), b(Q); rep(i, Q) { cin >> a[i] >> b[i]; a[i]--, b[i]--; } int ok = Q + 1, ng = 0; while (ok - ng > 1) { int md = (ok + ng) / 2; V> G(N); rep(i, md) G[a[i]].push_back(b[i]); V res = topological_sort(G); if (res.size() == N) { ng = md; } else { ok = md; } } cout << (ok == Q + 1 ? -1 : ok) << '\n'; return 0; }