#include <bits/stdc++.h>
using namespace std;

struct Point {
    int r, c;
};

int main()
{
    int n; cin >> n;
    struct Point p[n];

    int uRed[n] = {0};
    int uKol[n] = {0};

    for (int i = 0; i < n; ++i) {
        cin >> p[i].r >> p[i].c;
        p[i].r--; p[i].c--;
        uRed[p[i].r]++;
        uKol[p[i].c]++;
    }

    int sol = 0;

    // Zeros
    vector<int> nule;
    vector<int> nuleC;
    for (int i = 0; i < n; ++i) {
        if (!uRed[i]) nule.push_back(i);
        if (!uKol[i]) nuleC.push_back(i);
    }

    if (nule.empty()) goto kol;
    // rows
    for (int i = 0; i < n; ++i) {
        if (uRed[i] > 1) {

            while (uRed[i] > 1) {

                pair<int, int> m;
                m.first = (int) abs(i - nule.back());
                m.second = nule.back();

                int tmp = 0;
                for (int j = 0; j < nule.size(); ++j)
                    if ((int) abs(i - nule[j]) <= m.first) {
                        m.first = (int) abs(i - nule[j]);
                        m.second = nule[j];
                        tmp = j;
                    }

                sol += m.first;
                uRed[i]--;
                uRed[m.second]++;

                nule.erase(nule.begin()+tmp);
            }
        }
    }

    kol:
    // column
    for (int i = 0; i < n; ++i) {
        if (uKol[i] > 1) {

            while (uKol[i] > 1) {

                pair<int, int> m;
                m.first = (int) abs(i - nuleC.back());
                m.second = nuleC.back();

                int tmp = 0;
                for (int j = 0; j < nuleC.size(); ++j)
                    if ((int) abs(i - nuleC[j]) <= m.first) {
                        m.first = (int) abs(i - nuleC[j]);
                        m.second = nuleC[j];
                        tmp = j;
                    }

                sol += m.first;
                uKol[i]--;
                uKol[m.second]++;

                nuleC.erase(nuleC.begin()+tmp);
            }
        }
    }

    // Print solution
    cout << sol;

    return 0;
}