// 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.2608002499847888x):
#include <bits/stdc++.h>

typedef long long ll;

#define endl '\n'

#define pq priority_queue <pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> 

#define graph1 vector<vector<pair<ll,ll>>>

#define graph vector<vector<ll>>

#define pq1 priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll,ll>>>, greater<pair<ll, pair<ll, ll>>>>

#define loop(i,a,b) for (ll i = a; i < b; i++)

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

using namespace std;

bool visited[100005];

graph g;

ll maxx = 0;

void dfs(int node,  int dp[], bool vis[])

{

	

	vis[node] = true;



	for (int i = 0; i < g[node].size(); i++) {



		if (!vis[g[node][i]])

			dfs(g[node][i], dp, vis);



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

	}

}





int findLongestPath(int n)

{

	

	int dp[n + 1];

	memset(dp, 0, sizeof dp);



	

	bool vis[n + 1];

	memset(vis, false, sizeof vis);



	

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

		if (!vis[i])

			dfs(i, dp, vis);

	}



	int ans = 0;



	// Traverse and find the maximum of all dp[i] 

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

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

	}

	return ans;

}



int main()

{



	ll n, m;

	cin >> n >> m;

	g.resize(n + 1);

	memset(visited, false, sizeof(visited));

	loop(i, 0, m)

	{

		ll u, v;

		cin >> u >> v;

		g[u].push_back(v);

	}

	cout << findLongestPath(n);

}