#include <iostream>

typedef long long ll;

using namespace std;

ll brPoteza(ll niz[], ll n) {
    ll brojac = 0;

    for(ll i = 0; i < n; ++i) {
        if(niz[i] == 0) {
            int temp1 = i,temp2 = i;
            while(niz[temp1] <= 1 && niz[temp2] <= 1) {
                if(temp1 > 0) temp1--;
                if(temp2 < n-1) temp2++;
            }

            if(niz[temp1] > 1) {
                brojac += i - temp1;
                niz[temp1] -= 1;
            } else if(niz[temp2] > 1) {
                brojac += temp2 - i;
                niz[temp2] -= 1;
            }
        }
    }

    return brojac;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    ll n,t1,t2;
    cin >> n;

    ll nizH[n] = {0};
    ll nizV[n] = {0};

    for(ll i = 0; i < n; ++i) {
        cin >> t1 >> t2;
        nizH[t1-1] += 1;
        nizV[t2-1] += 1;
    }


    cout << brPoteza(nizH,n) + brPoteza(nizV,n);

    return 0;
}