//Let's join Kaede Takagaki Fan Club !! #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include #include #include using namespace std; typedef long long ll; typedef pair P; typedef pair P1; typedef pair P2; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define fi first #define sc second #define rep(i,x) for(int i=0;i void dmp(T a){ rep(i,a.size()) cout << a[i] << " "; cout << endl; } template bool chmax(T&a, T b){ if(a < b){ a = b; return 1; } return 0; } template bool chmin(T&a, T b){ if(a > b){ a = b; return 1; } return 0; } template void g(T &a){ cin >> a; } template void o(const T &a,bool space=false){ cout << a << (space?' ':'\n'); } //ios::sync_with_stdio(false); const ll mod = 998244353; template void add(T&a,T b){ a+=b; if(a >= mod) a-=mod; } mapM; int n, m; map>E; vectorvec; int main(){ cin >> n >> m; rep(i, m){ int a, b; cin >> a >> b; vec.pb(a); vec.pb(b); E[b].pb(a); } SORT(vec); ERASE(vec); reverse(all(vec)); rep(i, vec.size()){ int q = vec[i]; queueque; que.push(q); while(!que.empty()){ int q = que.front(); que.pop(); if(M.find(q) != M.end()) continue; M[q] = vec[i]; for(auto at:E[q]){ if(M.find(at) == M.end()) que.push(at); } } } ll ans = 1LL*n*(n+1)/2; rep(i, vec.size()) ans += M[vec[i]] - vec[i]; cout << ans << endl; }