#include <bits/stdc++.h>

struct Polje{
    int x;
    int y;
};

Polje a[300007];
int n;

using namespace std;
bool tmp (Polje a, Polje b) {
    if (a.x != a.y)
        return a.x < b.x;
    return a.y < b.y;
}
int main () {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a >> n;
        tabla[i].x = a;
        tabla[i].y = b;
    }

    sort(a, a + n, tmp);

    for (int i = 0; i < n; i++) {
        zb+=(a[i].x - i - 1) + (a[i].y - i - 1);
    }

    cout << zb << endl;
}