#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, dp[100005];
vector<int>node[100005];
bool vis[100005];

int solve(int a){
	if(vis[a]){
		return dp[a];
	}
	vis[a]=1;
	int total=0;
	for(auto i:node[a]){
		total=max(total, 1+solve(i));
	}
	return dp[a]=total;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie();
	cout.tie();
	cin >> n >> m;
	for(int i=1;i<=m;i++){
		cin >> x >> y;
		node[x].push_back(y);
	}
	int ha=0;
	for(int i=1;i<=n;i++){
 	   ha=max(ha, solve(i));
	}
	cout << ha << endl;
}