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

int n;
int rCnt[200005], cCnt[200005];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;

    for(int i = 0; i < n; ++i){
        int r, c;
        cin >> r >> c;
        --r; --c;

        ++rCnt[r]; ++cCnt[c];
    }

    int res = 0;
    int rCur = 0, cCur = 0;;
    for(int i = 0; i < n - 1; ++i){
        while(rCnt[i]){
            if(rCur == i){
                res += rCnt[i] - 1;
                rCnt[i + 1] += rCnt[i] - 1;
                ++rCur;
                break;
            }
            res += i - rCur;
            --rCnt[i];
            ++rCur;
        }

        while(cCnt[i]){
            if(cCur == i){
                res += cCnt[i] - 1;
                cCnt[i + 1] += cCnt[i] - 1;
                ++cCur;
                break;
            }
            res += i - cCur;
            --cCnt[i];
            ++cCur;
        }
    }

    cout << res << "\n";

    return 0;
}