#include<bits/stdc++.h>
using namespace std;
string ikun="woshixiaohezi";
const int maxn=1e6+10;
long long n,dp[maxn];
struct st{
long long h,w;
};
st a[maxn];
bool cmp(st x,st y){
if(x.h!=y.h){
return x.h<y.h;
}else{
return x.w<y.w;
}
}
long long f(long long l,long long r,long long ans,long long step){
if(dp[step]!=0)return dp[step];
if(step==n+1)return ans;
if(a[step].h>l&&a[step].w>r)dp[step]=max(dp[step],max(f(l,r,ans,step+1),f(a[step].h,a[step].w,ans+1,step+1)));
else dp[step]=max(dp[step],f(l,r,ans,step+1));
}
int main(){
ios::sync_with_stdio(false);
std::cin.tie();
std::cout.tie();
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].h>>a[i].w;
}
sort(a+1,a+1+n,cmp);
f(0,0,0,2);
f(a[1].w,a[1].h,1,2);
cout<<dp[n];
return 0;
}