#include <iostream>

using namespace std;

int p[200001], q[200001];
static long long h[200001], v[200001];

int stavi1h( int i )
{
    int j = 1;
    while( h[i+1] == 0 )
        j++;
    h[i] = 1;
    h[i+j]--;
    return j;
}

int stavi1v( int i )
{
    int j = 1;
    while( v[i+1] == 0 )
        j++;
    v[i] = 1;
    v[i+j]--;
    return j;
}

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

    int n, r = 0;

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

    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 += stavi1h(i);
        }
        if( v[i] > 1 )
        {
            r += v[i] - 1;
            v[i+1] += v[i] - 1;
            v[i] = 1;
        }
        else if( v[i] == 0 )
        {
            r += stavi1v(i);
        }
    }

    cout << r << endl;

    return 0;
}