// Slower C++ solution:
#include <bits/stdc++.h> 

using namespace std;



const int N = 1e5 + 100;





#define mp make_pair

#define f first

#define s second



#define pb push_back



void boost()

{

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);

}



int n,m;



vector<int>g[N];



bool was[N];



int d[N];



vector<int>ord;

int ans;



void dfs(int v)

{

 	was[v] = 1;

 	for (int to :g[v])

 	{

 		if (!was[to])

 		{

 			dfs(to);

 		}

 	}

 	ord.pb(v);

}

                                                                                                          

void dfs1(int v)

{

 	was[v] = 1;

 	for (int to :g[v])

 	{

 		if (!was[to])

 		{

 			dfs1(to);

 		}                               

 		d[v] = max(d[v],d[to] + 1);

 		ans = max(ans,d[v]);

 	}                          

}





int main(){

	boost();

	cin >> n >> m;

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

	{

		int u,v;

		cin >> u >> v;

		g[u].pb(v);

	}

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

	{

		if (!was[i])

		{             

			dfs(i);

		}

	}

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

	{

		was[i] = 0;

	}

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

	{                        

		dfs1(ord[i]);            

	}

	cout << ans;       

}

// Faster C++ solution for the same problem (measured speedup: 2.515964089360042x):
/*

██╗  ██╗ █████╗ ██████╗ ███████╗███████╗███╗   ███╗        ██╗  ██╗██╗  ██╗ █████╗ ██╗     ███████╗██████╗

██║ ██╔╝██╔══██╗██╔══██╗██╔════╝██╔════╝████╗ ████║        ██║ ██╔╝██║  ██║██╔══██╗██║     ██╔════╝██╔══██╗

█████╔╝ ███████║██████╔╝█████╗  █████╗  ██╔████╔██║        █████╔╝ ███████║███████║██║     █████╗  ██║  ██║

██╔═██╗ ██╔══██║██╔══██╗██╔══╝  ██╔══╝  ██║╚██╔╝██║        ██╔═██╗ ██╔══██║██╔══██║██║     ██╔══╝  ██║  ██║

██║  ██╗██║  ██║██║  ██║███████╗███████╗██║ ╚═╝ ██║███████╗██║  ██╗██║  ██║██║  ██║███████╗███████╗██████╔╝

╚═╝  ╚═╝╚═╝  ╚═╝╚═╝  ╚═╝╚══════╝╚══════╝╚═╝     ╚═╝╚══════╝╚═╝  ╚═╝╚═╝  ╚═╝╚═╝  ╚═╝╚══════╝╚══════╝╚═════╝

Believe in yourself ,( try and try and then try ), You can do it

*/

#define _CRT_SECURE_NO_WARNINGS

#include<bits/stdc++.h>

#include<unordered_map>

#define endl "\n"

#define Ceil(x,y) ((x+y-1)/y)

#define clr(arr, x) memset(arr, x, sizeof arr)

#define all(v) v.begin(),v.end()

#define allr(s) s.rbegin(),s.rend()

#define rt(s) return cout<<s,0

#define watch(x) cout<<(#x)<<" = "<<x<<endl

#define sz(s)	(int)(s.size())

#define OO 0x3f3f3f3f3f3f3f3fLL

const int oo = 0x3f3f3f3f, mod = 1e9 + 7;

using namespace std;

typedef long long ll;

typedef long double ld;

typedef unsigned long long ull;

int dx[] { 1, -1, 0, 0, 1, 1, -1, -1 };

int dy[] { 0, 0, 1, -1, 1, -1, 1, -1 };

void K_K(){ 

	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

}

vector<vector<int>>gr;

int dp[100005];

int dfs(int node){

	if (!sz(gr[node]))

		return 0;

	int &ret = dp[node];

	if (~ret)

		return ret;

	for (auto child : gr[node]){

		ret = max(ret, dfs(child) + 1);

	}

	return ret;

}

int main(){

	K_K();

	int n, m;

	cin >> n >> m;

	gr.resize(n + 1);

	clr(dp, -1);

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

		int u, v;

		cin >> u >> v;

		gr[u].push_back(v);

	}

	int ans = 0;

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

		dfs(i);

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

	}

	cout << ans;

}