#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
struct Fenwick
{
int n;
VI t;
void init(int _n)
{
n = _n;
t.assign(n, 0);
}
void upd(int i, int x)
{
for (; i < n; i |= i + 1)
t[i] = max(t[i], x);
}
int query(int i)
{
int ans = 0;
for (; i >= 0; i = (i & (i + 1)) - 1)
ans = max(ans, t[i]);
return ans;
}
} fn;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<PII> v(n);
VI a;
FOR (i, 0, n)
{
cin >> v[i].F >> v[i].S;
a.PB(v[i].F);
a.PB(v[i].S);
}
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
fn.init(SZ(a));
sort(ALL(v), [](PII x, PII y)
{
if (x.S == y.S)
return x.F > y.F;
return x.S < y.S;
});
FOR (i, 0, n)
{
int j = lower_bound(ALL(a), v[i].F) - a.begin();
int dp = fn.query(j - 1);
fn.upd(j, dp + 1);
}
cout << fn.query(SZ(a) - 1) << '\n';
return 0;
}