#include <bits/stdc++.h>

using namespace std;

#define int ll
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pi> vp;
typedef vector<pl> vpl;

set<int> s;

signed main()
{
    ios_base::sync_with_stdio(false); 
    cin.tie(nullptr); 
    cout.tie(nullptr); 
    cerr.tie(nullptr);    

    int n;
    cin >> n;

    vi a(n + 5, 0), b(n + 5, 0);
    for (int i = 0; i < n; ++i){
        int x, y;
        cin >> x >> y;

        ++a[x];
        ++b[y];
    }

    int solx = 0;

    for (int i = 1; i <= n; ++i){
        if (a[i] > 0)
            s.insert(i);
    }

    for (int i = 1; i <= n; ++i){
        if (a[i] == 1){
            continue;
        }

        if (a[i] > 1){
            a[i + 1] += a[i] - 1;
            solx += a[i] - 1;
            a[i] = 1;
            s.insert(i + 1);
            continue;
        }

        //infrastruktura
        //da bude lepo na oko
        if (!a[i]){
            int ind = *s.lower_bound(i);
            a[i] = 1;
            solx += ind - i;
            --a[ind];

            if (!a[ind]){
                s.erase(ind);
            }
        }
    }

    s.clear();

    int soly = 0;

    for (int i = 1; i <= n; ++i){
        if (b[i] > 0)
            s.insert(i);
    }

    for (int i = 1; i <= n; ++i){
        if (b[i] == 1){
            continue;
        }

        if (b[i] > 1){
            b[i + 1] += b[i] - 1;
            soly += b[i] - 1;
            b[i] = 1;
            s.insert(i + 1);
            continue;
        }

        //infrastruktura
        //da bude lepo na oko
        if (!b[i]){
            int ind = *s.lower_bound(i);
            b[i] = 1;
            soly += ind - i;
            --b[ind];

            if (!b[ind]){
                s.erase(ind);
            }
        }
    }

    int sol = solx + soly;
    cout << sol << '\n';
}