#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int **a = new int*[n + 1];
    int *br = new int[n + 1];
    for(int i = 1; i <= n; i++){
        a[i] = new int[n + 1];
        br[i] = 0;
    }
    for(int q = 1; q <= n; q++)
        for(int p = 1; p <= n; p++)
            a[q][p] = -1;
    int r;
    int c;
    for(int j = 1; j <= n; j++){
        cin >> r;
        cin >> c;
        a[r][c] = 0;
        br[r]++;
    }
    int vreme = 0;
    for(int h = 1; h <= n; h++){
        if(br[h] == 1) continue;
        else if(br[h] == 0){
            int g = h + 1;
            while(br[g] == 0 && g <= n) g++;
            int s = 1;
            while(a[g][s] != 0) s++;
            a[h][s] = 0;
            a[g][s] = -1;
            vreme += g - h;
            br[g]--;
        }
        else for(int v = 1; v <= n; v++){
            if(a[h][v] == 0){
                br[h]--;
                int m = h + 1;
                while(a[m][v] == 0 && m <= n) m++;
                a[m][v] = 0;
                a[h][v] = -1;
                br[m]++;
                vreme += m - h;
            }
            if(br[h] == 1) break;
        }
    }
    cout << vreme;
    return 0;
}