#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,r,c,g=0,potez,jm,jv;
    int x[200001]={0};
    int y[200001]={0};
    cin >> n;
    for (int i=0;i<n;i++){
        cin >> r >> c;
        x[r]++;
        y[c]++;
    }
    for (int i=1;i<=n;i++){
        if (x[i]==0){
            jm=i-1;
            jv=i+1;
            potez=1;
            while(jm>0 || jv<=n){
                if (x[jm]>1 && jm>0){
                    g+=potez;
                    x[jm]--;
                    x[i]++;
                    break;
                } else if (x[jv]>1 && jv<=n){
                    g+=potez;
                    x[jv]--;
                    x[i]++;
                    break;
                }
                if (jm>1)
                    jm--;
                if(jv<n)
                jv++;
                potez++;
            }
        }
        if (y[i]==0){
            jm=i-1;
            jv=i+1;
            potez=1;
            while(jm>0 || jv<=n){
                if (y[jm]>1 && jm>0){
                    g+=potez;
                    y[jm]--;
                    y[i]++;
                    break;
                } else if (y[jv]>1 && jv<=n){
                    g+=potez;
                    y[jv]--;
                    y[i]++;
                    break;
                }
                if (jm>1)
                    jm--;
                if(jv<n)
                jv++;
                potez++;
            }
        }
    }
    cout << g << endl;
    return 0;
}