#include<iostream>
#include<map>
using namespace std;
map<int, pair<int,int> > m1;
map<int, pair<int,int> > :: iterator it;
int p;
int find_pos(int ar[], int low, int high, int curr)
{
p=-1;
int mid;
while(low<high)
{
mid= (low+high)/2;
if(m1[mid].first <= m1[curr].first && m1[mid].second<=m1[curr].second)
{
low=mid+1;
p=low;
}
else if(m1[mid].first >= m1[curr].first && m1[mid].second>=m1[curr].second)
{
high=mid-1;
p=high;
}
}
if(p==-1)
return mid;
else return p;
}
int main()
{
int n,minx=1000000000,miny=1000000000,pos;
cin>>n;
int c[n+1],cnt=0;
int x,y,i;
for(i=0;i<n;i++)
{
cin>>x>>y;
if(x<=minx)
{
minx=x;
if(y<miny) {
miny=y;
pos=i;
}
}
m1.insert(pair<int, pair<int,int> >(i, make_pair(x, y)));
}
/*
cout<<"the entry with least value is "<<minx<<" "<<miny<<"at position "<<pos<<endl;
cout<<"The map has entries\n";
for(i=0;i<n;i++)
{
cout<<m1[i].first<<" "<<m1[i].second<<endl;
}
*/
i=0;
int sz=0;
c[sz]=pos;
while(i<n)
{
if(i==pos) i++;
if(m1[i].first>=m1[c[sz]].first && m1[i].second>=m1[c[sz]].second)
{
sz=sz+1;
cnt++;
c[sz]=i;
}
else if(m1[i].first<=m1[c[sz]].first && m1[i].second<=m1[c[sz]].second)
{
int p = find_pos(c,0,sz,i);
if(p!=-1) // indicates that the element can be inserted in the array
{
if(m1[c[p]].first<m1[i].first && m1[c[p]].second< m1[i].second)
p=p+1;
c[p]=i;
}
}
i++;
}
cout<<cnt<<endl;
//cout<<"length of lis is "<<cnt<<endl;
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPG1hcD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiBtYXA8aW50LCBwYWlyPGludCxpbnQ+ID4gbTE7CiBtYXA8aW50LCBwYWlyPGludCxpbnQ+ID4gOjogaXRlcmF0b3IgaXQ7CiAKIGludCBwOwogCmludCBmaW5kX3BvcyhpbnQgYXJb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