#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define mp make_pair

using namespace std;

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;
    
    set<int> col, row;
    int r_cnt[n], c_cnt[n];
    for(int i = 0; i < n; i++) {
        col.insert(i);
        row.insert(i);
        r_cnt[i] = c_cnt[i] = 0;
    }
    
    vector< pair<int, int> > coordinates;
    for(int i = 0; i < n; i++) {
        int r, c;
        cin >> r >> c;
        r--;
        c--;

        coordinates.pb(mp(r, c));
        r_cnt[r]++;
        c_cnt[c]++;
        col.erase(c);
        row.erase(r);
    }

    ll sol = 0;
    for(pair<int, int> k: coordinates) {
        int r = k.first, c = k.second;

        if(r_cnt[r] <= 1 && c_cnt[c] <= 1)
            continue;
        int x = 0, y = 0;
        if(r_cnt[r] > 1) {
            r_cnt[r]--;
            auto id = row.lower_bound(r);
            int a = -1, b = -1;
            if(id == row.begin())
                a = *id;
            else if(id == row.end())
                b = *(--id);
            else
                a = *id, b = *(--id);
            if(b == -1) {
                x = a - r;
                r_cnt[a]++;
                row.erase(a);
            } else if(a == -1) {
                x = r - b;
                r_cnt[b]++;
                row.erase(b);
            } else {
                if(a - r < r - b) {
                    x = a - r;
                    r_cnt[a]++;
                    row.erase(a);
                } else {
                    x = r - b;
                    r_cnt[b]++;
                    row.erase(b);
                }
            }
        }
        if(c_cnt[c] > 1) {
            c_cnt[c]--;
            auto id = col.lower_bound(c);
            int a = -1, b = -1;
            if(id == col.begin())
                a = *id;
            else if(id == col.end())
                b = *(--id);
            else
                a = *id, b = *(--id);
            if(b == -1) {
                y = a - c;
                c_cnt[a]++;
                col.erase(a);
            } else if(a == -1) {
                y = c - b;
                c_cnt[b]++;
                col.erase(b);
            } else {
                if(a - c < c - b) {
                    y = a - c;
                    c_cnt[a]++;
                    col.erase(a);
                } else {
                    y = c - b;
                    c_cnt[b]++;
                    col.erase(b);
                }
            }
        }

        sol += x + y;
    }

    cout << sol << endl;
    return 0;
}