#include<bits/stdc++.h>
using namespace std;

#define pb push_back

//var 
int n,m,t1,t2,dp[100001],ans = 0;
vector<int> e[100001];
bool v[100001];

void dfs(int g){
	dp[g] = 1; int am = 0;
	v[g] = 1;
	for(int u : e[g]){
		if(!v[u])dfs(u);
		am = max(am,dp[u]);
	}dp[g] += am;
}

int main(){
	cin >> n >> m;
	for(int i = 1; i<=m; i++){
		cin >> t1 >> t2;
		e[t1].pb(t2);
	}

	for(int i = 1; i<=n; i++){
		if(!v[i])dfs(i); ans = max(ans,dp[i]);
	}cout << --ans << endl;
}