#include <iostream>
#include <vector>

using namespace std;

typedef unsigned long int uli;
uli n;
unsigned int ukupno = 0;
vector<uli> pox;
vector<uli> poy;
vector<vector<bool>> mapa;

long int getCoinx(uli u);
long int getCoiny(uli u);
void nextx(uli x1, uli y1);
void nexty(uli x1, uli y1);

void uredix(uli x) {
    //cout << "pox[x]: " << pox[x] << endl;
    if (pox[x] == 0) {
        //cout << " x0" << endl;
        uli xx = 0;
        uli yy = 0;
        for (uli i = x+1; i < n; i++) {
            if (pox[i] > 0) {
                yy = getCoinx(i);
                xx = i;
                break;
            }
        }
        mapa[xx][yy] = false;
        pox[xx]--;
        mapa[x][yy] = true;
        pox[x]++;
        ukupno+= xx - x;
    }
    else if (pox[x] > 1) {
        //cout << "x1" << endl;
        while (pox[x] >1) {
                //cout << "AAA" << endl;
            nextx(x, getCoinx(x));
        }
    }
}
void urediy(uli y) {
    if (poy[y] == 0) {
        uli xx = 0;
        uli yy = 0;
        //cout << "yeeeee, uli y(argument): " << y << endl;
        for (uli i = y+1; i < n; i++) {
            if (poy[i] > 0) {
                xx = getCoiny(i);
                yy = i;
                break;
            }
        }
        //cout << "s " << xx << " i " << yy << " na " << xx << " i " << y << endl;
        mapa[xx][yy] = false;
        poy[yy]--;
        mapa[xx][y] = true;
        poy[y]++;
        ukupno+= yy- y;
    }
    else if (poy[y] > 1) {
        while (poy[y] >1) {
            nexty(getCoiny(y), y);
        }
    }
}

void nextx(uli x1, uli y1) {
    bool d = mapa[x1+1][y1];
    mapa[x1][y1] = false;
    mapa[x1+1][y1] = true;
    pox[x1]--;
    pox[x1+1]++;
    ukupno++;
    //cout << "d je: " << d << endl;
    if (d) nextx(x1+1, y1);
}

void nexty(uli x1, uli y1) {
    //bool d = map[x1+1][y1];
    mapa[x1][y1] = false;
    mapa[x1][y1+1] = true;
    poy[y1]--;
    poy[y1+1]++;
    ukupno++;
    //if (d) next(x1+1, y1);
}

long int getCoinx(uli u) {
    uli i = 0;
    bool oke = false;
    for (; i<n; i++) {
        if (mapa[u][i]) {
            oke = true;
            break;
        }
    }
    if (oke) return i;
    else return -1;
}

long int getCoiny(uli u) {
    //cout << "getCoiny je pozvan s " << u << endl;
    uli i = 0;
    bool oke = false;
    for (; i<n; i++) {
            //cout << "coin y loopira!" << endl;
        if (mapa[i][u]) {
            //cout << "yeet naso nesto!" << endl;
            oke = true;
            break;
        }
    }
    if (oke) return i;
    else return -1;
}

int main()
{
    std::ios::sync_with_stdio(false);

    cin >> n;

    vector<bool> vt;
    vt.assign(false, n);
    mapa.assign(n, vt);
    pox.assign(n, 0);
    poy.assign(n, 0);
    for (uli i = 0; i < n; i++) {
        uli x,y;
        cin >> x >> y;
        mapa[x-1][y-1] = true;
        pox[x-1]++;
        poy[y-1]++;
    }


    for (uli i = 0; i< n-1; i++) {
        uredix(i);
        //cout << "ukupno je trenutrno: " << ukupno << endl;
    }
    //cout << "uredjeno po x, ukupno: " << ukupno << endl;
    for (uli i = 0; i< n-1; i++) {
        urediy(i);
    }
    cout << ukupno << endl;
    /*
    uli slobodno = 0;
    bool yeet = false;
    for (uli i = 0; i < n; i++) {

        if (pox[i] == 0 && !yeet) {
                slobodno++;
                break;
        }
        else if (pox[i] !=0) yeet = true;
        for (; slobodno >0; slobodno--) {
            uli cx = slobodno;
            for (uli k = 0; k< n; k++) {
                if (map[k][slobodno] == true) {
                    cx = k;
                    break;
                }
            }
            next(cx, slobodno, true);
        }

        while (pox[i] >1) {
            uli cx = 0;
            for (uli k = 0; k< n; k++) {
                if (map[k][i] == true) {
                    cx = k;
                    break;
                }
            }
            next(cx, i, false);
        }
    }
*/

    return 0;
}