#include #include #include using namespace atcoder; using mint = modint1000000007; using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000001 int main(){ int N,K; cin>>N>>K; scc_graph S(N); vector x(K),y(K); bool f = false; rep(i,K){ cin>>x[i]>>y[i]; x[i]--;y[i]--; if(x[i]==y[i])f = true; S.add_edge(x[i],y[i]); } if(f){ cout<<-1< pos(N); rep(i,ret.size()){ pos[ret[i][0]] = i; } vector> E(N); rep(i,K){ E[pos[x[i]]].push_back(pos[y[i]]); } vector dp(N,0); rep(i,N){ rep(j,E[i].size()){ int to = E[i][j]; dp[to] = max(dp[to],dp[i]+1); } } int ans = 0; rep(i,N)ans = max(ans,dp[i]); cout<