#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<vector> 
#include<cstring>
#include<string>
#include<map>
#define mp make_pair
#define scn second
#define frs first
#define pb push_back

#define NAME "dominoes"

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pi;


int n;
bool cmp(pi a, pi b) {
    return a.scn < b.scn || (a.scn == b.scn && a.frs < b.frs);
}

int main(){
    freopen(NAME ".in", "r", stdin); 
    freopen(NAME ".out", "w", stdout); 
    scanf("%d", &n);
    vector<pi> a(n);
    for (int i = 0; i < n; ++i) {
        scanf("%d%d", &a[i].frs, &a[i].scn);        
    }
    sort(a.begin(), a.end(), cmp);
    map<int, int> ans;
    int answer = 0;
    for (int i = 0; i < n; ++i) {
        ans[a[i].scn] = max(ans[a[i].scn], ans[a[i].frs] + 1);
        answer = max(answer, ans[a[i].scn]);
    }
    printf("%d", answer);
    return 0;
}




