#include using namespace std; #define ALL(x) x.begin(),x.end() #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<bool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b ostream &operator<<(ostream &os,const pair&p){ os< ostream &operator<<(ostream &os,const vector&v){ for(int i=0;i<(int)v.size();i++) os< istream &operator>>(istream &is,pair&p){ is>>p.first>>p.second; return is; } template istream &operator>>(istream &is,vector&v){ for(T &x:v)is>>x; return is; } struct StronglyConnectedComponents{ vector> g; vector> to,from; vector comp,order,used; StronglyConnectedComponents(vector> &g): g(g),to(g.size()),from(g.size()),comp(g.size(),-1),used(g.size()){ for(int i=0;i> build(){ vector> t; for(int i=0;i>n>>m; vector> g(n); rep(i,m){ int u,v;cin>>u>>v;u--,v--; g[u].push_back(v); } StronglyConnectedComponents scc(g); auto t=scc.build(); vector mx(t.size(),0); rep(i,n)chmax(mx[scc[i]],i); for(int i=(int)t.size()-1;i>=0;i--){ for(auto j:t[i]) chmax(mx[i],mx[j]); } int ans=0; rep(i,n) ans+=1+mx[scc[i]]; cout<