#include <bits/stdc++.h>

using namespace std;
const int N = 1e5;
int arr[2][N*2] = {};
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, result = 0;
    int a, b;
    cin >> n;
    for(int i=0 ;i<n; i++)
    {
        cin >> a >> b;
        arr[0][a-1]++;
        arr[1][b-1]++;
    }
    for(int j=0; j<2; j++)
    {
        int car = 0, fil = 0;
        for(int i = 0; i<n; i++)
        {
            //cout << fil << " " << car << endl;
            result += fil + car;
            if(arr[j][i] == 1)
            {
                continue;
            }
            else if(arr[j][i] == 0)
            {
                if(car > 0) car--;
                else fil++;
            }
            else
            {
                a = arr[j][i]-1;
                if(fil <= a)
                {
                    a -= fil;
                    fil = 0;
                }
                car += a;
            }
        }

    }
    return cout << result << endl, 0;
}