#include <stdio.h>

long int red[200000];
long int kolona[200000];

int max(int a,int b){
if(a>b)
    return a;
return b;
}

int main()
{
    long int n,r,k,mark=-1;
    scanf("%ld",&n);
    for(long int i=0;i<n;i++){
        scanf("%ld %ld",&r,&k);
        red[r-1]++;
        kolona[k-1]++;
    }
    long long int p=0;
    for(long int i=0;i<n;i++){
        if(red[i]>1){
            if(i>=mark){
                red[i+1]+=red[i]-1;
                p+=red[i]-1;
                red[i]=1;
            }
            else{
                red[i+1]+=red[i]-1;
                p-=red[i]-1;
                red[i]=1;
            }
        }
        else if(red[i]==0){
            for(long int j=max(mark+1,i+1);j<n;j++){
                if(red[j]>1){
                    mark=j;
                    red[i]+=red[j]-1;
                    p+=(red[j]-1)*(j-i);
                    red[j]=1;
                    i--;
                    j=n;
                }
            }
        }
    }
    mark=-1;
    for(long int i=0;i<n;i++){
        if(kolona[i]>1){
            if(i>=mark){
                kolona[i+1]+=kolona[i]-1;
                p+=kolona[i]-1;
                kolona[i]=1;
            }
            else{
                kolona[i+1]+=kolona[i]-1;
                p-=kolona[i]-1;
                kolona[i]=1;
            }
        }
        else if(kolona[i]==0){
            for(long int j=max(mark+1,i+1);j<n;j++){
                if(kolona[j]>1){
                    mark=j;
                    kolona[i]+=kolona[j]-1;
                    p+=(kolona[j]-1)*(j-i);
                    kolona[j]=1;
                    i--;
                    j=n;
                }
            }
        }
    }
    printf("%lld",p);
    return 0;
}