#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod 1000000007
ll dmod(ll x){
    return ((x+mod)%mod);
}
ll modular_power(ll x,ll y){
    ll ans=1;
    while(y){
        if(y&1)ans=dmod(ans*x);
        y/=2;
        x=dmod(x*x);
    }
    return ans;
}
vector <int> v[3000];
set <int> st;
int ans;
void dfs(int x){
    if(st.count(x))return;
    st.insert(x);
    ans++;
    for(auto node:v[x]){
        dfs(node);
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int a,b;
        cin>>a>>b;
        v[a].push_back(b);
    }
    ans=0;
    for(int i=1;i<=n;i++){
        st.clear();
        dfs(i);
    }
    cout<<ans;
    return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <set>
#include <stdio.h>
#include <string.h>
#include <map>
#include <queue>
#include <math.h>
using namespace std;
typedef long long ll;
ll n, a, b, s, res;
int main(){
    cin>>n;
    s = 0;
    res = 0;
    for(int i = 0; i < n; ++i){
        cin>>a>>b;
        s = (b * (b + 1)) / 2;;
        s -= (a * (a + 1)) / 2;
        s += a;
        res += s;
    }
    cout<<res<<endl;
    return 0;
}
