// Original C++ solution (slower):
#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);

 } 


// Optimized solution to the same problem (faster):
#include <iostream>

#include <string>

#include<iomanip>

#include <algorithm>

#include <map>

#include <vector>

#include<queue>

#include<math.h>

#include<cmath>

#include<sstream>

#include<set>

#include <map>

#include<string.h>

#include<sstream>

#include<stack>

#include<unordered_map>

#include <iostream>

#include <stdio.h>

#include <ctype.h>

#include <cstdio>

#include <cstdlib>

#define ll long long

#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define arr2dof vector  vector<vector<int>>dp(N,vector<int>(2, inf));

using namespace std;

const int N = 2e5 + 5, inf = 1e9+7 ;

const double ep = 0.0000000001;

const double pi = 3.14159265359;

const long long inff= 1e18;

vector < vector<int>>graph;

int n, m,a,b;

int dp[100005];

bool visited[100005];

void dfs(int node) {

	visited[node] = true;

	for (int child : graph[node]) {

		if (!visited[child]) 

		dfs(child);

		

		

			dp[node] = max(dp[node], 1 + dp[child]);

		

	}

}





int main()

{

	

	cin >> n>>m;

	graph.resize(max(n,m) + 5);

	

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

		cin >> a >> b;

		graph[a].push_back(b);

	

	}

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

		if (!visited[i])

			dfs(i);

	}

	int ans = 0;

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

		ans = max(ans, dp[i]);

	}

	cout << ans;

	return 0;

}

// Fastest known solution for this problem: