#include <iostream>
#include <vector>

using namespace std;
const int N = 1e5+1;

vector < int > g[N];
int dp[N];

int dfs(int v) {
	if(dp[v]) return dp[v];
	if(g[v].size() == 0) return 0;
	for(int u : g[v]) {
		dp[v] = max(dp[v], dfs(u) + 1);
	}

	return dp[v];
}

int main() {
	int n, m;
	cin >> n >> m;
	for(int i = 0; i < m; i++) {
	  int a, b; cin >> a >> b;
		g[a].push_back(b);
	}

	int ans = 0;
	for(int i = 1; i <= n; i++) {
		if(dp[i] == 0) {
			dfs(i);
			ans = max(ans, dp[i]);
		}
	}

	cout << ans << endl;
}