#include <iostream>

using namespace std;

int n, a[200005][2], axis[200005][2], tp,pt;
int moveCount = 0;

int req(int j, int i){
    if(i > n)
        return i;
    if(axis[i][j] == 0){
        return req(j,i + 1);
    }
    return i;
}

int reqL(int j, int i){
    if(i < 0)
        return i;
    if(axis[i][j] == 0){
        return req(j,i - 1);
    }
    return i;
}

int main()
{
    std::ios::sync_with_stdio(false);

    cin >> n;

    for(int i = 0;i < n;i++){
        cin >> tp >> pt;
        a[i][0] = tp;
        a[i][1] = pt;
        axis[tp][0]++;
        axis[pt][1]++;
    }

    if(n == 1){
        cout << 0;
        return 0;
    }
    if(n == 2){
        if(a[0][0] == a[1][0] || a[0][1] == a[1][1]){
            cout << 1;
            return 0;
        }else{
            cout << 0;
            return 0;
        }
    }

    int j = 0;
    for(int j = 0;j < 2;j++){
        //cout << "Sleva \n";

        for(int x = 1;x <= n;x++){
            if(axis[x][j] == 0){
                int tmp = req(j,x + 1);
                //Pronadji igraca sa poklapajucom adresom
                for(int igrac = 0;igrac < n;igrac++){
                    if(a[igrac][j] == tmp){
                        //cout << a[igrac][0] << "-" << a[igrac][1] << " -> ";

                        a[igrac][j] = x;
                        axis[tmp][j]--;
                        axis[x][j]++;

                        //cout << a[igrac][0] << "-" << a[igrac][1] << endl;
                        moveCount++;

                        break;
                    }
                }
            }
        }
        //cout << "Sdesna \n";

        for(int x = n;x > 0;x--){
            if(axis[x][j] == 0){
                int tmp = reqL(j,x - 1);
                //Pronadji igraca sa poklapajucom adresom
                for(int igrac = 0;igrac < n;igrac++){
                    if(a[igrac][j] == tmp){
                        //cout << a[igrac][0] << "-" << a[igrac][1] << " -> ";

                        a[igrac][j] = x;
                        axis[tmp][j]--;
                        axis[x][j]++;

                        //cout << a[igrac][0] << "-" << a[igrac][1] << endl;
                        moveCount++;

                        break;
                    }
                }
            }
        }
    }
/*
    for(int i = 0;i < n;i++){
        cout << a[i][0] << " " << a[i][1] << endl;
    }
*/
    cout << moveCount << endl;

    return 0;
}