#include <iostream>
#include<bits/stdc++.h>
using namespace std;
//nezavisno posmatranje po redovima i kolonama
deque<int> qrm,qrv,qcm,qcv;
int brr[200010],brc[200010],pozv;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,i,r,c,j,br;
    long long t=0;
    cin >> n;
    for(i=0;i<n;i++)
    {
        cin >> r >> c;
        brr[r]++;
        brc[c]++;
    }
    //cout << qrm.size() << endl;
    for(i=1;i<=n;i++)
    {
        if(brr[i]>1)
        {
            if(qrm.empty()==false)
            {
                br=(int)qrm.size();
                for(j=0;j<min(br,brr[i]-1);j++)
                {
                    t=t+(long long)i-(long long)qrm.front();
                    qrm.pop_front();
                    brr[i]--;
                }
                if(brr[i]>1)
                    qrv.push_back(i);
            }
            else
                qrv.push_back(i);
        }
        else
        {
            if(brr[i]==0)
            {
                if(qrv.empty()==false)
                {
                    t=t+(long long)i-(long long)qrv.front();
                    brr[qrv.front()]--;
                    if(brr[qrv.front()]==1)
                        qrv.pop_front();
                }
                else
                    qrm.push_back(i);
            }
        }
    }
    for(i=1;i<=n;i++)
    {
        if(brc[i]>1)
        {
            if(qcm.empty()==false)
            {
                br=(int)qcm.size();
                for(j=0;j<min(br,brc[i]-1);j++)
                {
                    t=t+(long long)i-(long long)qcm.front();
                    qcm.pop_front();
                    brc[i]--;
                }
                if(brc[i]>1)
                    qcv.push_back(i);
            }
            else
                qcv.push_back(i);
        }
        else
        {
            if(brc[i]==0)
            {
                if(qcv.empty()==false)
                {
                    t=t+(long long)i-(long long)qcv.front();
                    brc[qcv.front()]--;
                    if(brc[qcv.front()]==1)
                        qcv.pop_front();
                }
                else
                    qcm.push_back(i);
            }
        }
    }
    cout << t;
    //cout << "Hello world!" << endl;
    return 0;
}