#include #include #include #include #include #include #include using namespace std; typedef long long ll; ll N, M; map> G; set used; ll ans; bool is_used(ll m){ return used.count(m) > 0; } void dfs(ll v, ll r){ used.insert(v); if(r > v) ans += r-v; for(ll to : G[v]){ if(!is_used(to)) dfs(to, r); } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; cin >> N >> M; vector v; for(int i = 0; i < M; i++){ ll b, c; cin >> b >> c; G[c].push_back(b); v.push_back(c); } ans = (N*(N+1))/2; sort(v.begin(), v.end(), greater()); for(ll i : v) { if(!is_used(i)) dfs(i, i); } cout << ans << endl; }