#include <iostream>
#include <vector>

using namespace std;

int n;
int colin;
int rowin;
int read;
int delta = 0;
int firstCheck = 0;
int margin = 0;

int steps = 0;

int main()
{
    cin >> n;

    vector<bool> fullRow(n,0);
    vector<bool> fullColumn(n, 0);
    
    typedef vector<vector<int>> intMatrix;

    vector<int> x(n,0);
    intMatrix map(n,x);

    for (int i = 0; i < n; i++) {
        cin >> rowin >> colin;
        map[rowin-1][colin-1] = 1;
    }

    for (int i = 0; i < n; i++) {
        firstCheck = 0;
        for (int j = 0; j < n; j++) {
            read = map[i][j];
            if (read != 0) {
                fullColumn[j] = 1;
                fullRow[i] = 1;
                if (firstCheck == 0) {
                    firstCheck = 1;
                }
                else {
                    for (int k = i; k >= 0; k--) {
                        delta = 0;
                        margin = 0;

                        if (fullRow[k] == 0) {
                            map[k][j] = 1;
                            map[i][j] = 0;
                            fullRow[k] = 1;
                            delta = i - k;
                            break;
                        }

                    }
                        if(delta == 0) {
                            margin = 0;
                            int l = i;
                            while (l < n && map[l][j] == 1) {
                                margin++;
                                l++;
                            }
                            map[i + margin][j] = 1;
                            map[i][j] = 0;
                            fullRow[i + margin] = 1;
                           
                            
                        }
                    
                    steps = steps + delta + margin;


                    delta = 0;
                    margin = 0;
                }
            }
        }
    }
    

    read = 0;
    delta = 0;
    firstCheck = 0;
    margin = 0;

    for (int j = 0; j < n; j++) {
        firstCheck = 0;
        for (int i = 0; i < n; i++) {
            read = map[i][j];
            if (read != 0) {
                fullColumn[j] = 1;
                fullRow[i] = 1;
                if (firstCheck == 0) {
                    firstCheck = 1;
                }
                else {
                    for (int k = j; k >= 0; k--) {
                        delta = 0;
                        margin = 0;

                        if (fullColumn[k] == 0) {
                            map[i][k] = 1;
                            map[i][j] = 0;
                            fullColumn[k] = 1;
                            delta = j - k;
                            break;
                        }

                    }
                    if (delta == 0) {
                        margin = 0;
                        int l = j;
                        while (l < n && map[i][l] == 1) {
                            margin++;
                            l++;
                        }
                        map[i][j + margin] = 1;
                        map[i][j] = 0;
                        fullColumn[j + margin] = 1;


                    }

                    steps = steps + delta + margin;


                    delta = 0;
                    margin = 0;
                }
            }
        }
    }



    cout << steps;
}