#include #include using namespace std; using namespace atcoder; #define reps(i,s,n) for(int i = s; i < n; i++) #define rep(i,n) reps(i,0,n) #define Rreps(i,n,e) for(int i = n - 1; i >= e; --i) #define Rrep(i,n) Rreps(i,n,0) #define ALL(a) a.begin(), a.end() using ll = long long; using vec = vector; using mat = vector; ll N,M,H,W,Q,K,A,B; string S; using P = pair; using tp = tuple; const ll INF = (1LL<<60); template bool chmin(T &a, const T b){ if(a > b) {a = b; return true;} else return false; } template bool chmax(T &a, const T b){ if(a < b) {a = b; return true;} else return false; } template void my_printv(std::vector v,bool endline = true){ if(!v.empty()){ for(std::size_t i{}; i; void no(){ cout<<-1<>N>>K; mat G(N + 1); rep(i, K){ int r, c; cin>>r>>c; --r; --c; if(r == c) no(); G[r].push_back(c); } { vec edge_num(N, 0); for(vec &v : G) for(int to : v) ++edge_num[to]; rep(i, N) if(edge_num[i] == 0) G[N].push_back(i); } queue que; vec dp(N + 1, 0); que.push(N); while(!que.empty()){ int v = que.front(); que.pop(); for(int to : G[v]){ if(chmax(dp[to], dp[v] + 1)){ que.push(to); } } } cout<<(*max_element(ALL(dp)))<