#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <ll,ll> p;
const int N = 2e5+45;

ll n;
ll a[N];

ll pomeri(){
    ll cost = 0;
    ll l = 1;
    for(ll i = 1; i <= n; i++){
        if(a[i]){
            if(i <= l){
                cost += (l-i)*a[i]+((a[i]-1)*a[i])/2;
            }
            else{ /// l < i
                if(l+a[i]-1 <= i){
                    cost += (i-l)*a[i]-((a[i]-1)*a[i])/2;
                }
                else{
                    cost += ((i-l)*(i-l+1))/2;
                    cost += ((l+a[i]-1-i)*(l+a[i]-i))/2;
                }
            }
            l += a[i];
        }
    }
    return cost;
}

void nes(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    cout << pomeri() << endl;
}

void zad(){
    cin >> n;
    vector <p> v;
    for(int i = 1; i <= n; i++){
        int x,y;
        cin >> x >> y;
        v.push_back(p(x,y));
        a[y]++;
    }
    ll sol = pomeri();
    for(int i = 1; i <= n; i++){
        a[i] = 0;
    }
    for(auto f : v){
        a[f.first]++;
    }
    sol += pomeri();
    cout << sol << endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    zad();
}