#include <iostream>
#include <cmath>

using namespace std;

int xbr[200005], ybr[200005];
int xkol[200005], ykol[200005];

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i){
        int x, y;
        cin >> x >> y;
        xbr[x-1]++;
        ybr[y-1]++;
    }
    long long resbr = 0;
    int brx = 0;
    for (int i = 0; i < n; ++i){
        if (xbr[i] != 1){
            xkol[brx++] = i;
        }
    }
    int res = 0;
    for (int i = 0; i < brx; ++i){
        if (xbr[xkol[i]] > 1){
            int cnt = 0;
            for (int j = res; cnt < xbr[xkol[i]] - 1; ++j){
                if (xbr[xkol[j]] > 1){
                    continue;
                }
                resbr += abs(xkol[i] - xkol[j]);
                cnt++;
            }
            res += xbr[xkol[i]] - 1;
        }
    }
    int bry = 0;
    for (int i = 0; i < n; ++i){
        if (ybr[i] != 1){
            ykol[bry++] = i;
        }
    }
    res = 0;
    for (int i = 0; i < bry; ++i){
        if (ybr[ykol[i]] > 1){
            int cnt = 0;
            for (int j = res; cnt < ybr[ykol[i]] - 1; ++j){
                if (ybr[ykol[j]] > 1){
                    continue;
                }
                resbr += abs(ykol[i] - ykol[j]);
                cnt++;
            }
            res += ybr[ykol[i]] - 1;
        }
    }
    cout << resbr;
    return 0;
}