#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
ll i;
bool nif(ll niz[],ll n){
    for(i=0;i<n-1;++i){
        for(ll j=i+1;j<n;++j){
            if(niz[i]==niz[j]){
                return false;
            }
        }
    }
    return true;
}
bool nif1(ll niz[],ll n,ll br){
    for(i=0;i<n;++i){
        if(br==niz[i]){
            return false;
        }
    }
    return true;
}
int main(){
    ll n,niz1[100000],niz2[100000],br=0,exs=0,exs1=0,exs2=0,exs3=0;
    cin>>n;
    for(i=0;i<n;++i){
        cin>>niz1[i]>>niz2[i];
    }
    for(i=0;i<n;++i){
    	exs=niz1[i]+1;
        exs1=niz1[i]-1;
        exs2=niz2[i]+1;
        exs3=niz2[i]-1;
        if(nif(niz1,n)){
            cout<<"0";
            return 0;
        }
        else{
            if(exs<=3 and nif1(niz1,n,exs)){
                niz1[i]+=1;
                ++br;
            }
            else if(exs2<=3 and nif1(niz2,n,exs2)){
                niz2[i]+=1;
                ++br;
            }
            else if(exs1>=1 and nif1(niz1,n,exs1)){
                niz1[i]-=1;
                ++br;
            }
            else if(exs3>=1 and nif1(niz2,n,exs3)){
                niz2[i]-=1;
                ++br;
            }
            else if(exs<=3 and nif1(niz1,n,exs) and exs2<=3 and nif1(niz2,n,exs2)){
            	niz1[i]+=1;
            	niz2[i]+=1;
            	++br;
			}
			else if(exs<=3 and nif1(niz1,n,exs) and exs3>=1 and nif1(niz2,n,exs3)){
				niz1[i]+=1;
				niz2[i]-=1;
				++br;
			}
			
			else if(exs1>=1 and nif1(niz1,n,exs1) and exs2<=3 and nif1(niz2,n,exs2)){
				niz1[i]-=1;
				niz2[i]+=1;
				++br;
			}
			else if(exs1>=1 and nif1(niz1,n,exs1) and exs3>=1 and nif1(niz2,n,exs3)){
				niz1[i]-=1;
				niz2[i]-=1;
				++br;
			}
        }
        if(nif(niz1,n)){
            cout<<br;
            return 0;
        }
    }
    return 0;
}