#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

typedef long long ll;

ll suma(long *a, long n)
{
    ll sum=0;
    vector<long> v1,v2;
    for (long i=0;i<n;i++)
        if (a[i]==0) v1.push_back(i); else v2.push_back(i);
    long m=v2.size();
    long cur=0;
    for (long i=0;i<m;i++)
    {
        long ind=v2[i];
        for (long j=cur;j<cur+a[ind]-1;j++)
            sum+=abs(ind-v1[j]);
        cur=cur+a[ind]-1;
    }
    return sum;
}

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

    long n;
    cin >> n;
    long *a=(long*)calloc(n,sizeof(long));
    long *b=(long*)calloc(n,sizeof(long));
    for (long i=0;i<n;i++)
    {
        long r,c;
        cin >> r >> c;
        --r;
        --c;
        a[r]++;
        b[c]++;
    }
    cout << suma(a,n)+suma(b,n);
    delete[] a;
    delete[] b;
    return 0;
}