#include <iostream>
#include<algorithm>
#include<cmath>

using namespace std;

long long int a[200005],b[200005];

int main()
{
    ios::sync_with_stdio(false);
    long long int i,j,n,res=0,t,x,y;

    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>x>>y;
        a[x]++;  b[y]++;
    }

    t=0;
    for(i=1;i<=n;i++)
    {
        t+=a[i]-1;
        res+=abs(t);
       // cout<<a[i]<<" "<<t<<endl;
    }

    //cout<<res<<endl;

    t=0;
    for(i=1;i<=n;i++)
    {
        t+=b[i]-1;
        res+=abs(t);
    }


    cout<<res<<endl;

    return 0;
}