#include <iostream>

using namespace std;

static long long h[200001], v[200001];

int stavi1( long long int a[], int i )
{
    int j = 1;
    while( a[i+j] == 0 )
        j++;
    a[i] = 1;
    a[i+j]--;
    return j;
}

int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, r = 0;

    cin >> n;
    int p, q;
    for( int i = 1; i <= n; i++ )
    {
        cin >> p >> q;
        h[p]++;
        v[q]++;
    }

    for( int i = 1; i <= n; i++ )
    {
        if( h[i] > 1 )
        {
            r += h[i] - 1;
            h[i+1] += h[i] - 1;
            h[i] = 1;
        }
        else if( h[i] == 0 )
        {
            r += stavi1(h, i);
        }
        if( v[i] > 1 )
        {
            r += v[i] - 1;
            v[i+1] += v[i] - 1;
            v[i] = 1;
        }
        else if( v[i] == 0 )
        {
            r += stavi1(v, i);
        }
    }

    cout << r << endl;

    return 0;
}