#include <bits/stdc++.h>

using namespace std;

int r[200001];
int c[200001];
int zamena = 0;

bool proveri(int n, int t, int m)
{
    if(!m)
    {
        for(int i = 1; i <= n; i++)
        {
            if(r[i] == t || c[i] == t) return true;
        }
    }
    else

    {
        for(int i = 1; i <= n; i++)
        {
            if(r[i] > t || c[i] > t) return true;
        }
    }
    return false;
}

void zameni(int x, int m, int n)
{
    if(m == 0)
    {
        if(r[x+1] > 1)
        {
            r[x+1]--;
            r[x]++;          
             zamena++;
        }   
        else if(r[x-1] > 1)
        {
            r[x-1]--;
            r[x]++;
            zamena++;
        }
        else if(proveri(x, 1, 1)) zameni(x-1, 0, n);
        else zameni(x+1, 0, n);
    }
    else
    {
        for(int i = x+1; i <= n; i++)
        {
            if(c[i] <= 1) zameni(i, 1, n);
            else
            {
                c[i]--;
                c[i-1]++;
                zamena++;
            }
        }
    }

    return;
}

int main()
{
    int n, r1, c1; cin>>n;

    for(int i = 1; i <= n; i++)
    {
        cin>>r1>>c1;
        r[r1]++; c[c1]++;
    }

    int j = 1, br = 1;

    while(proveri(n, 0, 0))
    {
        if(r[j] == 0)
        {
            zameni(j, 0, n);
        }
        if(c[j] == 0) zameni(j, 1, n);

        if(j == n) j = 0;
        j++;
        br++;
    }



    cout<<zamena;

    return 0;
}