#include <bits/stdc++.h>
using namespace std;
const int N = 3e4 + 1;
int n , dp[N];
vector < int > e[N];
int recur ( int time ){
if ( time >= N ) return 0;
int &ret = dp[time];
if ( ~ret ) return ret;
ret = recur ( time + 1 );
for ( auto child : e[time] ){
ret = max ( ret , 1 + recur ( child + 1 ) );
}
return ret;
}
void Solution ( int tc ){
cin >> n;
for ( int i = 1 ; i <= n ; i++ ){
int st;
int en;
cin >> st >> en;
e[st].push_back ( en );
}
cout << recur ( 1 ) << "\n";
return;
}
int main()
{
/// MUHAMMAD
ios::sync_with_stdio(0);
cin.tie(0);
/// AE;
/*
#ifdef OJ
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
*/
int testcase = 1 , tc = 0;
for ( int i = 0 ; i < N ; ++i ){
dp[i] = -1;
}
/// cin >> testcase;
for ( int i = 1 ; i <= testcase ; ++i ){
Solution( ++tc );
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBOID0gM2U0ICsgMTsKCmludCBuICwgZHBbTl07CnZlY3RvciA8IGludCA+IGVbTl07CgppbnQgcmVjdXIgKCBpbnQgdGltZSApewoKICAgaWYgKCB0aW1lID49IE4gKSByZXR1cm4gMDsKCiAgIGludCAmcmV0ID0gZHBbdGltZV07CgogICBpZiAoIH5yZXQgKSByZXR1cm4gcmV0OwoKICAgcmV0ID0gcmVjdXIgKCB0aW1lICsgMSApOwoKICAgZm9yICggYXV0byBjaGlsZCA6IGVbdGltZV0gKXsKICAgICByZXQgPSBtYXggKCByZXQgLCAxICsgcmVjdXIgKCBjaGlsZCArIDEgKSApOwogICB9CgogICByZXR1cm4gcmV0Owp9Cgp2b2lkIFNvbHV0aW9uICggaW50IHRjICl7CgogIGNpbiA+PiBuOwoKICBmb3IgKCBpbnQgaSA9IDEgOyBpIDw9IG4gOyBpKysgKXsKICAgIGludCBzdDsKICAgIGludCBlbjsKICAgIGNpbiA+PiBzdCA+PiBlbjsKICAgIGVbc3RdLnB1c2hfYmFjayAoIGVuICk7CiAgfQoKICBjb3V0IDw8IHJlY3VyICggMSApIDw8ICJcbiI7CgogIHJldHVybjsKfQoKaW50IG1haW4oKQp7CiAgICAvLy8gTVVIQU1NQUQKICAgIGlvczo6c3luY193aXRoX3N0ZGlvKDApOwogICAgY2luLnRpZSgwKTsKCiAgICAvLy8gQUU7CgogICAgLyoKICAgICNpZmRlZiBPSgogICAgICAgIGZyZW9wZW4oImlucHV0LnR4dCIsICJyIiwgc3RkaW4pOwogICAgICAgIGZyZW9wZW4oIm91dHB1dC50eHQiLCAidyIsIHN0ZG91dCk7CiAgICAjZW5kaWYKICAgICovCgogICAgaW50IHRlc3RjYXNlID0gMSAsIHRjID0gMDsKCiAgICBmb3IgKCBpbnQgaSA9IDAgOyBpIDwgTiA7ICsraSApewogICAgICAgIGRwW2ldID0gLTE7CiAgICB9CgogICAgLy8vIGNpbiA+PiB0ZXN0Y2FzZTsKCiAgICBmb3IgKCBpbnQgaSA9IDEgOyBpIDw9IHRlc3RjYXNlIDsgKytpICl7CiAgICAgICBTb2x1dGlvbiggKyt0YyApOwogICAgfQoKICAgIHJldHVybiAwOwp9Cg==