#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define si short int
#define vi vector<ll>
#define vii vector<vi>

ll nadji(ll a[], ll index, ll n);

int main(void)
{
    ll n;
    cin>>n;
    ll red[n+2]={0},kolone[n+2]={0};
    ///////////////////////////////////////////////////////////
    ///da ne zaboravim da mi je niz od 111111111111111111111111
    ///////////////////////////////////////////////////////////
    for(ll i=0;i<n;i++)
    {
        ll x,y;
        cin>>x>>y;
        red[x]++;
        kolone[y]++;
    }
    ll brc=0;
    for(ll i=1;i<=n;i++)
    {
        if(red[i]==0)
            brc+=nadji(red,i,n);
        if(kolone[i]==0)
            brc+=nadji(kolone,i,n);
    }
    cout<<brc;
}

ll nadji(ll a[], ll index, ll n)
{
    ll levo=index-1;
    bool flag=false;
    while(levo>=1 && !flag)
    {
        if(a[levo]>=2)
        {
            a[levo]--;
            a[index]=1;
            flag=true;
        }
        else
            levo--;
    }
    if(flag==true) return index-levo;
    ll desno=index+1;
    while(desno<=n && !flag)
    {
        if(a[desno]>=2)
        {
            a[desno]--;
            a[index]=1;
            flag=true;
        }
        else
            desno++;
    }
    return desno-index;
}