#include <bits/stdc++.h>
#define MAXN 200005
using namespace std;

int col[MAXN];
int row[MAXN];
stack<int> daj;
stack<int> ost;

int main()
{ int rr,cc,n,i;
long long sol=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
    scanf("%d%d",&rr,&cc);

    col[cc]++;

    row[rr]++;
}

for(i=1;i<=n;i++)
{
    while(col[i]>1)
    {
        col[i]--;

        daj.push(i);
    }

    if(col[i]==0) ost.push(i);

    while( (!ost.empty()) && (!daj.empty()) )
    {
        int tmp1=ost.top(); int tmp2=daj.top();

        sol=sol+(long long)abs(tmp1-tmp2);

        ost.pop(); daj.pop();
    }
}

while(!ost.empty()) ost.pop();

while(!daj.empty()) daj.pop();

for(i=1;i<=n;i++)
{
    while(row[i]>1)
    {
        row[i]--;

        daj.push(i);
    }

    if(row[i]==0) ost.push(i);

    while( (!ost.empty()) && (!daj.empty()) )
    {
        int tmp1=ost.top(); int tmp2=daj.top();

        sol=sol+(long long)abs(tmp1-tmp2);

        ost.pop(); daj.pop();
    }
}

printf("%lld\n",sol);
    return 0;
}