#include <bits/stdc++.h>

using namespace std;

int brnax[200010]= {0};
int brnay[200010]= {0};


int odpozadi(int n,int m)
{
    int rez=0;

    if(m==1)
    {
        int brnula=0;

        for(int i=n; i>0; i--)
        {
            if(brnax[i]==0)
            {
                brnula++;
            }
            else
            {
                if(brnax[i]<=brnula+1)
                {
                    brnula-=brnax[i]-1;
                }
                else
                {
                    brnax[i-1]+=brnax[i]-brnula-1;
                    rez+=brnax[i]-brnula-1;
                    brnax[i]=brnula+1;
                    brnula=0;
                }
            }
        }
    }
    else
    {
        int brnula=0;

        for(int i=n; i>0; i--)
        {
            if(brnay[i]==0)
            {
                brnula++;
            }
            else
            {
                if(brnay[i]<=brnula+1)
                {
                    brnula-=brnay[i]-1;
                }
                else
                {
                    brnay[i-1]+=brnay[i]-brnula-1;
                    rez+=brnay[i]-brnula-1;
                    brnay[i]=brnula+1;
                    brnula=0;
                }
            }
        }
    }

    return rez;
}


int odnaprijed(int n,int m)
{
    int rez=0;

    if(m==1)
    {
        int brnula=0;

        for(int i=1; i<=n; i++)
        {
            if(brnax[i]==0)
            {
                brnula++;
            }
            else
            {
                if(brnax[i]<=brnula+1)
                {
                    brnula-=brnax[i]-1;
                }
                else
                {
                    brnax[i+1]+=brnax[i]-brnula-1;
                    rez+=brnax[i]-brnula-1;
                    brnax[i]=brnula+1;
                    brnula=0;
                }
            }
        }
    }
    else
    {
        int brnula=0;

        for(int i=1; i<=n; i++)
        {
            if(brnay[i]==0)
            {
                brnula++;
            }
            else
            {
                if(brnay[i]<=brnula+1)
                {
                    brnula-=brnay[i]-1;
                }
                else
                {
                    brnay[i+1]+=brnay[i]-brnula-1;
                    rez+=brnay[i]-brnula-1;
                    brnay[i]=brnula+1;
                    brnula=0;
                }
            }
        }
    }

    rez+=odpozadi(n,m);

    return rez;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n,r,c,m=1,rez=0;

    cin >> n;

    for(int i=1; i<=n; i++)
    {
        cin >> r;
        cin >> c;

        brnax[r]++;
        brnay[c]++;
    }

    rez+=odnaprijed(n,m);
    m++;
    rez+=odnaprijed(n,m);

    cout << rez << endl;

    return 0;
}