#include #include #include #include using namespace std; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; #define rep(i,n) for(int i=0; i<(n); i++) #include vector> SCC(const vector>& E){ int n = E.size(); vector F(n, 0); vector St; vector I; for(int i=0; i> R(n); vector> G; for(int i=0; i=0; i--){ int s = I[i]; if(F[s] == 0) continue; vector g; St.push_back(s); while(St.size()){ int p = St.back(); St.pop_back(); if(F[p] == 0) continue; F[p] = 0; g.push_back(p); for(int e : R[p]) St.push_back(e); } G.emplace_back(move(g)); } for(int i=0; i> N; int K; cin >> K; if(K == 0){ cout << "0\n"; return 0; } vector> E(N); rep(i,K){ int u,v; cin >> u >> v; u--; v--; if(u == v){ cout << "-1\n"; return 0; } E[u].push_back(v); } auto G = SCC(E); if(G.size() != N){ cout << "-1\n"; return 0; } vector D(N,1); int ans = 1; for(auto& A : G) for(auto p : A) for(int e : E[p]) D[e] = max(D[e], D[p]+1); rep(i,N) ans = max(ans,D[i]); cout << ans << endl; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;