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

int nizx[200001];
int nizy[200001];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
            nizx[i]=0;
            nizy[i]=0;
    }
    for(int i=0;i<n;i++)
    {
        int x,y;
        scanf("%d %d",&x,&y);
        nizx[x-1]++;
        nizy[y-1]++;
    }
    //x-udesno
    int rez=0,brnul=0;
    for(int i=0;i<n;i++){
        int x=nizx[i];

        if(x==0)brnul++;
        else{
            if(brnul+1<x){

                if(i!=n-1){

                    nizx[i+1]+=x-brnul-1;
                    rez+=x-brnul-1;
                    x=brnul+1;
                }
                brnul=0;
            }else brnul-=x-1;
        }
        nizx[i]=x;
        //cout<<nizx[i]<<" ";
    }
   // cout<<endl;
    //x-ulevo
    brnul=0;
    for(int i=n-1;i>=0;i--){
        int x=nizx[i];

        if(x==0)brnul++;
        else{
            if(brnul+1<x){

                if(i!=0){

                    nizx[i-1]+=x-brnul-1;
                    rez+=x-brnul-1;
                    x=brnul+1;
                }
                brnul=0;
            }else brnul-=x-1;
        }
        nizx[i]=x;
       // cout<<nizx[i]<<" ";
    }
   // cout<<endl;
    //y-udesno
    brnul=0;
    for(int i=0;i<n;i++){
            
        int x=nizy[i];

        if(x==0)brnul++;
        else{
            if(brnul+1<x){
                //cout<<i<<" "<<brnul<<" "<<x<<endl;

                if(i!=n-1){

                    nizy[i+1]+=x-brnul-1;
                    rez+=x-brnul-1;
                    x=brnul+1;
                }
                 brnul=0;
            }else brnul-=x-1;
        }
        nizy[i]=x;
    }
    //y-ulevo
     brnul=0;
    for(int i=n-1;i>=0;i--){
            
        int x=nizy[i];

        if(x==0)brnul++;
        else{
            if(brnul+1<x){

                if(i!=0){

                    nizy[i-1]+=x-brnul-1;
                    rez+=x-brnul-1;
                    x=brnul+1;
                }
                 brnul=0;
            }else brnul-=x-1;
        }
        nizy[i]=x;
    }
    printf("%d",rez);
    return 0;
}