// // Created by yamunaku on 2020/08/22. // #include using namespace std; #define rep(i, n) for(int i = 0; i < (n); i++) #define repl(i, l, r) for(int i = (l); i < (r); i++) #define per(i, n) for(int i = ((n)-1); i >= 0; i--) #define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--) #define all(x) (x).begin(),(x).end() #define MOD9 998244353 #define MOD1 1000000007 #define IINF 1000000000 #define LINF 1000000000000000000 #define SP <<" "<< #define CYES cout<<"Yes"<; using mti = vector>; using vl = vector; using mtl = vector>; using pi = pair; using pl = pair; template using heap = priority_queue, function>; map> e; map> ne; map id; map col; void dfs(int x, int &num){ id[x] = -2; for(auto &nx : e[x]){ if(id[nx] == -1){ dfs(nx, num); } } id[x] = num++; } void dfs2(int x, int c){ col[x] = c; for(auto &nx : ne[x]){ if(col[nx] == -1){ dfs2(nx, c); } } } int main(){ CFS; ll n; int m; cin >> n >> m; vi b(m), c(m); rep(i, m){ cin >> b[i] >> c[i]; e[b[i]].push_back(c[i]); ne[c[i]].push_back(b[i]); id[b[i]] = id[c[i]] = -1; col[b[i]] = col[c[i]] = -1; } int sz = 0; for(auto &p:e){ if(id[p.first] == -1){ dfs(p.first, sz); } } ll ans = n * (n + 1) / 2; vi a(sz); map ma; for(auto &p : id){ a[p.second] = p.first; ans -= p.first; } per(i, sz){ if(col[a[i]] == -1){ dfs2(a[i], a[i]); } } rep(i, sz){ ma[col[a[i]]] = max(ma[col[a[i]]], a[i]); } map> ee; rep(i, m){ ee[col[b[i]]].push_back(col[c[i]]); } set f; rep(i, sz){ int cc = col[a[i]]; if(f.find(cc) != f.end()) continue; f.insert(cc); for(auto &nx : ee[cc]){ ma[cc] = max(ma[cc], ma[nx]); } } rep(i, sz){ ans += ma[col[a[i]]]; } cout << ans << endl; return 0; }