// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <cmath>

#include <cstdlib>

#include <cstdio>

#include <cstring>

#include <iomanip>

#include <string>

#include <set>

#include <vector>

#include <queue>

#include <stack>

#include <map>

using namespace std;

#define ms(a, x) memset(a, x, sizeof(a))

#define fore(i, a, n) for (long long i = a; i < n; i++)

#define ford(i, a, n) for (long long i = n - 1; i >= a; i--)

#define si(a) scanf("%d", &a)

#define sl(a) scanf("%lld", &a)

#define sii(a, b) scanf("%d%d", &a, &b)

#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)

#define sll(a, b) scanf("%lld%lld", &a, &b)

#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)

#define debug(a) cout << a << endl

#define pr(a) printf("%d ",a)

#define endl '\n'

#define pi acos(-1.0)

#define tr t[root]

#define lson t[root << 1]

#define rson t[root << 1 | 1]

#define IO ios::sync_with_stdio(false), cin.tie(0)

#define ull unsigned long long

const double eps = 1e-8;

inline int sgn(const double &x) { return x < -eps ? -1 : x > eps; }

typedef long long ll;

const int inf=0x3f3f3f3f;

const int MAXN = 1e5 + 10;

int n,m;

struct edge{

	int to ,next;

}e[MAXN];

int head[MAXN];

int cnt,ans;

void add(int x,int y)

{

	e[cnt].to=y;

	e[cnt].next=head[x];

	head[x]=cnt;

	cnt++;

 }

int ru[MAXN],chu[MAXN],dp[MAXN];

ll dfs(int x)

{

	if(chu[x]==0) return 0;

	if(dp[x])return dp[x];

	ll ans=0;

	for(int i=head[x];~i;i=e[i].next )

	{

		ans=max(dfs(e[i].to)+1,ans);

				

	}//cout<<ans<<endl;

	

	return dp[x]=ans;

}

int main()

{

	sii(n,m);

	ms(head,-1);

	ll ans=0;

	for(int i=0;i<m;++i)

	{

		int x,y;

		sii(x,y);

		add(x,y);

		ru[y]++;

		chu[x]++;

	}

	for(int i=1;i<=n;++i)

	{

		if(!ru[i])

		{

			ans=max(ans,dfs(i));

		}

	}

	printf("%lld\n",ans);

 } 


// Faster C++ solution for the same problem (measured speedup: 1.3760951534409591x):
#include<bits/stdc++.h>

#define si(a) scanf("%d", &a)

#define fore(i,a,b) for(int i = a; i < b; ++i)

using namespace std;

typedef long long ll;

typedef pair<int,int> pi;

typedef vector<int> vi;

vector<vector<int> > gr;

vi out;

vi ans;

vector<bool> visited;

vi aux;

void dfs(int u){

	visited[u] = true;

	//ans[u] = max(ans[u], val);

	fore(j,0,gr[u].size()){

		if(!visited[ gr[u][j] ])

			dfs(gr[u][j]);

		//ans[u] = max(ans[u], ans[ gr[u][j] ] + 1);

	}

	aux.push_back(u);

}

void solve(){

	int n,m;

	si(n); si(m);

	int x,y;

	gr.assign(n+1,vector<int>());

	out.assign(n+1,0);

	ans.assign(n+1,0);

	vector<pi> inv(m);

	aux.reserve(n);

	vi in(n+1);

	visited.assign(n+1,false);

	fore(i,0,m){

		si(x);si(y);

		inv[i] = pi(x,y);

		out[x]++;

		in[y]++;

	}

	fore(i,1,n+1)

		gr[i].reserve( in[i] );

	fore(i,0,m){

		x = inv[i].first;

		y = inv[i].second;

		gr[x].push_back(y);

	}

		

	fore(i,1,n+1)

		if(!visited[i])

			dfs(i);

	vi dp(n+1);

	//printf("aux: ");for(int i:aux) printf("%d ", i);printf("\n");

	for(int v: aux){

		if(gr[v].size() == 0)

			dp[v] = 0;

		else{

			int k = 0;

			for(int u: gr[v])

				k = max(k, dp[u]);

			dp[v] = k+1;

		}

	}

	int anss = 0;

	//printf("dp: ");for(int i:dp) printf("%d ", i);printf("\n");

	

	for(int i: dp)

		anss = max(anss, i);

	

	printf("%d\n", anss);

}

int main(){

	solve();

	return 0;

}