#include<cmath>
#include<bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    int n,x,y,br;
    cin>>n;
    br=0;
    vector<long int> tabela;
    vector<long int> tabelarev;
    vector<long int> tabelay;
    vector<long int> tabelayrev;
	tabelayrev.assign(n,0);
	tabelay.assign(n,0);
	tabelarev.assign(n,0);
	tabela.assign(n,0);
    for (int i=0; i<n;i++)
	{
		cin>>x>>y;
		tabelay[y-1]++;
		tabela[x-1]++;		
	}
	for (int i=0; i<n;i++)
	{
		if(tabelay[i]==0 && tabelay[i+1]>0)
		{
			tabelay[i]++;
			tabelay[i+1]--;
			br++;
		}
		if(tabela[i]==0 && tabela[i+1]>0)
		{
			tabela[i]++;
			tabela[i+1]--;
			br++;
		}
				
	}
	for (int i=0; i<n;i++)
	{
		tabelayrev[i]=tabelay[n-1-i];
		tabelarev[i]=tabela[n-1-i];			
	}
	
	for (int i=0; i<n;i++)
	{
		if(tabelayrev[i]==0 && tabelayrev[i+1]>0)
		{
			tabelayrev[i]++;
			tabelayrev[i+1]--;
			br++;
		}
				
	}
	for (int i=0; i<n;i++)
	{
		if(tabelarev[i]==0 && tabelarev[i+1]>0)
		{
			tabelarev[i]++;
			tabelarev[i+1]--;
			br++;
		}
				
	}
	cout<<br;  
    return 0; 
}