#include using namespace std; using i64 = long long; #define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++) #define all(x) x.begin(),x.end() #define STRINGIFY(n) #n #define TOSTRING(n) STRINGIFY(n) #define PREFIX "#" TOSTRING(__LINE__) "| " #define debug(x) \ { \ std::cout << PREFIX << #x << " = " << x << std::endl; \ } std::ostream& output_indent(std::ostream& os, int ind) { for(int i = 0; i < ind; i++) os << " "; return os; } template std::ostream& operator<<(std::ostream& os, const std::pair& p); template std::ostream& operator<<(std::ostream& os, const std::vector& v); template std::ostream& operator<<(std::ostream& os, const std::pair& p) { return (os << "(" << p.first << ", " << p.second << ")"); } template std::ostream& operator<<(std::ostream& os, const std::vector& v) { os << "["; for(int i = 0;i < v.size();i++) os << v[i] << ", "; return (os << "]"); } template static inline std::vector ndvec(size_t&& n, T val) { return std::vector(n, std::forward(val)); } template static inline auto ndvec(size_t&& n, Tail&&... tail) { return std::vector(tail)...))>(n, ndvec(std::forward(tail)...)); } template struct chain { Cond cond; chain(Cond cond) : cond(cond) {} template bool operator()(T& a, const T& b) const { if(cond(a, b)) { a = b; return true; } return false; } }; template chain make_chain(Cond cond) { return chain(cond); } struct strongly_connected_componects { int n; std::vector>> G; std::vector> scc; std::vector low; std::vector num; std::vector st; std::vector in_st; int cnt; strongly_connected_componects(int n): n(n), G(n) {} void add_edge(int a, int b, int i) { G[a].emplace_back(b, i); } void visit(int v, int e) { low[v] = num[v] = ++cnt; st.emplace_back(v); in_st[v] = true; for(auto [t, ei]: G[v]) { if(ei >= e) continue; if(num[t] == 0) { visit(t, e); low[v] = std::min(low[v], low[t]); } else if(in_st[t]) { low[v] = std::min(low[v], num[t]); } } if(low[v] == num[v]) { std::vector vs; while(true) { int t = st.back(); st.pop_back(); in_st[t] = false; vs.emplace_back(t); if(v == t) break; } scc.emplace_back(std::move(vs)); } } void build_scc(int e) { low.assign(n, 0); num.assign(n, 0); st.clear(); st.reserve(n); in_st.assign(n, false); scc.clear(); cnt = 0; for(int i = 0;i < n;i++) { if(num[i] == 0) { visit(i, e); } } } }; int main() { i64 N, Q; cin >> N >> Q; strongly_connected_componects scc(N); rep(i,0,Q) { i64 a, b; cin >> a >> b; a--; b--; scc.add_edge(a, b, i); } scc.build_scc(Q); if(scc.scc.size() == N) { cout << -1 << endl; return 0; } i64 ok = Q; i64 ng = 0; while(ok - ng > 1) { i64 mid = (ok + ng) / 2; scc.build_scc(mid); if(scc.scc.size() == N) { ng = mid; } else { ok = mid; } } cout << ok << endl; }