#include using namespace std; //#define int long long #define rep(i,n) for(int i=0;i=0;i--) #define all(v) v.begin(),v.end() #define P pair #define len(s) (int)s.size() template inline bool chmin(T &a, T b){ if(a>b){a=b;return true;} return false; } template inline bool chmax(T &a, T b){ if(axx,G[400005],rG[400005]; bool used[400005]; vectorvs; void dfs(int x){ used[x]=true; for(int i:G[x])if(!used[i])dfs(i); vs.push_back(x); } int cmp[400005],mx[400005]; vectornewrG[400005]; void rdfs(int v,int k){ used[v]=true; cmp[v]=k; cnt[k]++;chmax(mx[k],xx[v]); for(int i:rG[v])if(!used[i])rdfs(i,k); } int scc(){ rep(i,N)if(!used[i])dfs(i); memset(used,0,sizeof(used)); int k=0; rev(i,len(vs))if(!used[vs[i]])rdfs(vs[i],k++); rep(i,N){ for(int j:G[i]){ newrG[cmp[j]].push_back(cmp[i]); } } return k; } signed main(){ cin.tie(0);ios::sync_with_stdio(false); cin>>N>>M; rep(i,M){ cin>>B[i]>>C[i]; xx.push_back(B[i]); xx.push_back(C[i]); } sort(all(xx));xx.erase(unique(all(xx)),xx.end()); Ans=(long long)(1+N)*N/2; for(int i:xx)Ans-=i; rep(i,M){ B[i]=lower_bound(all(xx),B[i])-xx.begin(); C[i]=lower_bound(all(xx),C[i])-xx.begin(); G[B[i]].push_back(C[i]); rG[C[i]].push_back(B[i]); } N=len(xx); N=scc(); rev(i,N){ Ans+=(long long)mx[i]*cnt[i]; for(int j:newrG[i])chmax(mx[j],mx[i]); } cout<