#include #include using namespace std; using namespace atcoder; typedef int64_t lint; #define rep(i, n) for(int i=0; i; using vvi = vector>; template inline void vin(vector& v) { rep(i, v.size()) cin >> v.at(i); } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template inline void drop(T x) { cout << x << endl; exit(0); } template void vout(vector v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; } constexpr lint LINF = LLONG_MAX/2; vi A, B; vi topo_sort(int N, int M) { vector> G(N); int x, y; rep(i, M) { x = A[i], y = B[i]; G[x].push_back(y); } vi res; vi v(N); rep(i, N) { for (auto e : G[i]) v[e]++; } queue Q; rep(i, N) { if (!v[i]) Q.push(i); } while (!Q.empty()) { int x = Q.front(); Q.pop(); res.push_back(x); for (auto e : G[x]) { v[e]--; if (!v[e]) Q.push(e); } } return res; } int main() { lint N, Q; cin >> N >> Q; lint a=0, b=Q+1, c=0, x, y, z; rep(i, Q) { cin >> x >> y; A.pb(x-1), B.pb(y-1); } while (b-a > 1) { c = (a+b)/2; vi v = topo_sort(N, c); if (v.size() == N) a = c; else b = c; } if (b == Q+1) std::cout << -1 << '\n'; else std::cout << b << '\n'; }