#include <bits/stdc++.h>
using namespace std;
int n,red,red2,kolona,kolona2,x[200010],y[200010],pos=1;
long long rez,pos2=1;
int main (){
//    freopen("unos2.txt","r",stdin);
//    freopen("izlaz.txt","w",stdout);
    scanf("%d",&n);
    if(n<=2){
        if(n==2){
            cin>>red>>kolona;
            cin>>red2>>kolona2;
            if(red!=red2 and kolona2!=kolona){cout << 0 << endl;return 0;}
            else{cout<<1<<endl;return 0;}
        }
        if(n==1){
            cin>>red>>kolona;
            cout << 0 << endl;
            return 0;
        }
    }
    for(int i = 1; i <= n; i++){
        scanf("%d %d",&red,&kolona);
        x[red]++;
        y[kolona]++;
    }
    int pozicija=1;
    for(int i = 1; i <= n; i++){
        if(x[i]==0 and i!=1){
            while(1){
                if(x[pos]>=2){
                    rez+=abs(pos-i);
                    x[pos]--;
                    break;
                }else{pos++;}
                if(pos==n){
                    pos=1;
                    while(1){
                        if(x[pos]>=2){
                            rez+=abs(pos-i);
                            x[pos]--;
                            break;
                        }else{pos++;}
                    }
                }
            }
        }
        if(y[i]==0 and i==1){
            while(1){
                if(y[pos2]>=2){
                    rez+=pos2-i;
                    y[pos2]--;
                    break;
                }else{pos2++;}
            }
        }
        if(y[i]==0 and i!=1){
            while(1){
                if(y[pos2]>=2){
                    rez+=abs(pos2-i);
                    y[pos2]--;
                    break;
                }else{pos2++;}
                if(pos2==n){
                    pos2=1;
                    while(1){
                        if(y[pos2]>=2){
                            rez+=abs(pos2-i);
                            y[pos2]--;
                            break;
                        }else{pos2++;}
                    }
                }
            }
        }
    }
    if(x[n]==0){
        for(int i = 1; i <= n-1; i++){
            if(x[i]>1){
                rez+=n-i;
                break;
            }
        }
    }
    if(y[n]==0){
        for(int i = 1; i <= n-1; i++){
            if(y[i]>1){
                rez+=n-i;
                break;
            }
        }
    }
    cout << rez << endl;
return 0;
}