stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 25, M = 1e6 + 25, oo = 1e9, mod = 1e9 + 7;

const long long OO = 1e18+25;



int n, m,u,v, mx, mem[N];

vector <int> g[N];



int dp(int p)

{

    int &ret = mem[p];

    if(ret != 0) return ret;

    for(int to : g[p])

        ret = max(ret, dp(to)+1);

    return ret;

}



int main()

{

    scanf("%d%d",&n,&m);

    while(m--)

    {

        scanf("%d%d",&u,&v);

        g[u].push_back(v);

    }

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

    {

        mx = max(mx, dp(i));

    }

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

    return 0;

}

	     		 					   	 	 								 	

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

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

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

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

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

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

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

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;

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

#define eps 1e-3

#define pi acos(-1.0)

#define inf 0x3f3f3f3f

#define debug1 cout<<"&&";

#define debug2 cout<<"**";

#define pb push_back

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

#define forn(i, n) for(int i = 0; i < n; ++i)

#define for1(i, n) for(int i = 1; i <= n; ++i) 

typedef double db;

typedef long long ll;

typedef unsigned long long ull;

using namespace std;

const int mod = 1e9;

const int N = 1e5+10;

/*=======================================================================*/



int n, m;

int dp[N], in[N];

vector<int>G[N];

queue<int>q;

int ans = 0;



int main() {

   int a, b;

   scanf("%d%d", &n, &m);

   forn(i, m) {

      scanf("%d%d", &a, &b);

      in[b]++;

      G[a].push_back(b);

   }

   for1(i, n) {

      if(!in[i]) q.push(i);

   }

   while(!q.empty()) {

      int now = q.front();

      q.pop();

      int sz = G[now].size();

      forn(i, sz) {

         dp[G[now][i]] = max(dp[G[now][i]], dp[now] + 1);

         in[G[now][i]]--;

         if(in[G[now][i]] == 0) q.push(G[now][i]);

      }

   }

   for1(i, n) {

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

   }

   cout << ans << endl;

}

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

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

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

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

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

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

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

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;

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

using namespace std;

const int N=100005;

int dp[N],vis[N];

vector<int>e[N],p;

void dfs(int v)

{

	vis[v]=1;

	for(int i=0;i<e[v].size();i++)	

		if(!vis[e[v][i]])

			dfs(e[v][i]);

	p.push_back(v);

}

int main()

{

	int n,m;

	scanf("%d%d",&n,&m);

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

	{

		int x,y;

		scanf("%d%d",&x,&y);

		e[x].push_back(y);

	}

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

		if(!vis[i])

			dfs(i);

	reverse(p.begin(),p.end());

	int ans=0;

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

	{

		int v=p[i];

		for(int j=0;j<e[v].size();j++)

			dp[e[v][j]]=max(dp[e[v][j]],dp[p[i]]+1);

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

	}

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

	return 0;

}


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

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

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

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

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

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

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

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;

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

using namespace std;



typedef long long ll;

typedef long double ld;

typedef pair<int,int> pii;

typedef pair<ll,ll> pll;



const int mx=1e5+5;



int dp[mx];

vector<int>g[mx];



int path(int u)

{

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

	if(g[u].size()==0) return dp[u]=0;

	for(auto it: g[u])

	{

		dp[u]=max(dp[u],1+path(it));

	}

	return dp[u];

}		



void solve()

{

	int n,m;

	cin>>n>>m;

	

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

	{

		int u,v;

		cin>>u>>v;

		g[u].push_back(v);

	}

	

	int ans=0;

	

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

	{

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

	}

	cout<<ans;

}



int main()

{

	ios::sync_with_stdio(false);

	cin.tie(0); cout.tie(0);

	

	solve();

							                                				

	cerr<<"\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms\n";

	

	return 0;

}

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

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

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

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

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

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

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

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;

}
// Slower C++ solution:
// Author : Syed Mujtaba

// Seek knowledge from the Cradle to the Grave !!

// https://www.linkedin.com/in/syed-mujtaba-064a5016a 



#include <bits/stdc++.h>

using namespace std;

#define int     long long

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

#define mod ((int)(1e9 + 7))

vector < int > to[100005];

vector < int > from[100005];

vector < int > roots;

int dp[100005];

int cal(int i)

{

	if(dp[i] != -1)

		return dp[i];

	int dist = -1;

	for(int j = 0; j < to[i].size(); j++)

	{

		dist = max(dist, 1 + cal(to[i][j]));

	}

	return dp[i] = dist;

}

signed main()

{

	fast_io

	int n, m;

	int x, y;

	cin >> n >> m;

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

	{	

		cin >> x >> y;

		to[y].push_back(x);

		from[x].push_back(y);

	}

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

		dp[i] = -1;

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

	{

		if(to[i].size() == 0)

		{

			roots.push_back(i);

			dp[i] = 0;

		}

	}

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

	{

		if(dp[i] == -1)

			dp[i] = cal(i);

	}

	int ans = -1;

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

	{

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

	}

	cout << ans << endl;

}

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

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

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

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

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

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

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

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;

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

using namespace std;

vector<int>adj[100005];

int dp[100005];

bool vis[100005];

void dfs(int u)

{

    vis[u]=true;

    for(auto v:adj[u])

    {

        if(vis[v]==false)

            dfs(v);

        dp[u]=max(dp[u],dp[v]+1);

    }





}

int main()

{

    int n,m;

    cin>>n>>m;

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

    {

        int u,v;

        cin>>u>>v;

        adj[u].push_back(v);

    }

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

    {

        if(vis[i]==false)

            dfs(i);

    }

    int ans=0;

    for(int i=1; i<=n; i++) ans=max(ans,dp[i]);

    cout<<ans<<endl;

}


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

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

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

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

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

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

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

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;

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

#include<iomanip>

#include<cmath>

#include<string>

#include<cctype>

#include<algorithm>

#include<set>

#include<map>

#include<stack>

#include<queue>

#include<cstring>

//#include<windows.h>

//#include<time.h>

#define pb push_back

#define ppb pop_back

#define fi first

#define se second

#define mer make_pair

#define mme memset

using namespace std;



const double pie1=3.1415926;//π1

const double pie2=3.14159265358979;//π2

const double pie3=3.14159265358979323846;//π3

const int vbi=999999999;

const int vli=-vbi;

const long long vbl=999999999999999999;

const long long vll=-vbl;



typedef long long ll;

typedef pair<int,int> pii;

typedef pair<ll,ll> pll;

typedef pair<pii,int> piii;

typedef pair<pii,pii> pi4;

typedef pair<pll,ll> plll;

typedef pair<int,pii> ipii;

typedef pair<ll,pll> lpll;

typedef pair<pll,pll> pl4;

typedef vector<int> vi;

typedef vector<ll> vl;

typedef vector<pii> vpi;

typedef vector<pll> vpl;

typedef set<int> sti;

typedef set<ll> stl;

typedef set<pii> spi;

typedef set<pll> spl;

priority_queue<int> pqi;

priority_queue<ll> pql;

queue<int> qi;

queue<ll> ql;



/*void bin_v(){

	char bin[1000];

	gets(bin);

}*/



bool pss(int a){

	for(int i=2;i<=sqrt(a);i++)

		if(a%i==0)

			return false;

	return true;

}

/////////////////////////////////////////////////////////////////////////////////////////

int n,m;

vector<int> v[100010];

int d[100010];

int dp[100010];



int main(){

	//my name is Veex_WCY

	ios::sync_with_stdio(false);

	cin>>n>>m;

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

		int a,b;

		cin>>a>>b;

		v[a].pb(b);

		d[b]++;

	}

	stack<int> st;

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

		if(d[i]==0)

			st.push(i);

	while(!st.empty()){

		int x=st.top();

		st.pop();

		for(int i=0;i<v[x].size();i++){

			int ne=v[x][i];

			if(dp[x]+1>dp[ne])

				dp[ne]=dp[x]+1;

			d[ne]--;

			if(d[ne]==0)

				st.push(ne);

		}

	}

	int maxn=0;

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

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

	cout<<maxn;

	return 0;

}


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

#define ll          long long int

#define pii         pair<int,int>

#define uu          first

#define vv          second

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

using namespace std;



const int MAX = 2e5 + 5;

const ll INF = 1e18;

int main()

{

    fastIO;

    int n, m;

    cin>>n>>m;

    

    vector< vector<int> >edg(n+1), rev(n+1);

    vector<int>deg(n+1), dp(n+1);

    

    while(m--) {

        

        int u , v;

        cin>>u>>v;

        edg[u].push_back(v);

        rev[v].push_back(u);

        deg[u]++;

    }

    

    queue<int>q;

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

        if(deg[i] == 0) q.push(i);

    }

    int ans = 0;

    

    while(!q.empty()) {

        

        int top = q.front();

        q.pop();

        for(auto x : edg[top]) dp[top] = max(dp[top] , dp[x] + 1);

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

        for(auto x : rev[top]) {

            deg[x]--;

            if(deg[x] == 0 ) q.push(x);

        }

    }

    cout<<ans<<'\n';

    return 0;

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

#define ll long long

using namespace std;

ll n,m,x,y;

vector<ll>v[100009];

ll dp[100009];



ll f(ll N){

	if (v[N].empty())return 0;

	if(dp[N]!=-1)return dp[N];

	ll z=0;

	for(int i=0; i<v[N].size(); i++){

		z=max(z,f(v[N][i])+1);

	}

	dp[N]=z;

	return dp[N];

}



int main(){

	cin >> n >> m;

	memset(dp,-1,sizeof dp);

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

		cin >> x >> y;

		v[x].push_back(y);

	}

	ll jwb=0;

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

		jwb=max(jwb,f(i));

	}

	cout << jwb << endl;

}//j

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

#define ll          long long int

#define pii         pair<int,int>

#define uu          first

#define vv          second

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

using namespace std;



const int MAX = 2e5 + 5;

const ll INF = 1e18;

int main()

{

    fastIO;

    int n, m;

    cin>>n>>m;

    

    vector< vector<int> >edg(n+1), rev(n+1);

    vector<int>deg(n+1), dp(n+1);

    

    while(m--) {

        

        int u , v;

        cin>>u>>v;

        edg[u].push_back(v);

        rev[v].push_back(u);

        deg[u]++;

    }

    

    queue<int>q;

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

        if(deg[i] == 0) q.push(i);

    }

    int ans = 0;

    

    while(!q.empty()) {

        

        int top = q.front();

        q.pop();

        for(auto x : edg[top]) dp[top] = max(dp[top] , dp[x] + 1);

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

        for(auto x : rev[top]) {

            deg[x]--;

            if(deg[x] == 0 ) q.push(x);

        }

    }

    cout<<ans<<'\n';

    return 0;

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



using namespace std;



#define ll long long int



int memo[(int)1e5 + 1];

vector<int> adj[(int)1e5 + 1];



bool vis[(int)1e5 + 1];



int dfs(int x) {

	

	if(vis[x])return memo[x];



	vis[x] = true;

	

	int temp = 1;



	for(int i = 0; i < adj[x].size(); i++) {

		memo[x] = max(memo[x], temp + dfs(adj[x][i]));

	}



	return memo[x];

}



int main() {



	ios_base::sync_with_stdio(0);

	cin.tie(0);

	

	int n,m; cin >> n >> m;

	int s,t;

	

	while(m--) {

		cin >> s >> t;

		adj[s].push_back(t);

	}



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

		dfs(i);

	}



	int ans = 0;

	

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

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

	}



	cout << ans << '\n';



	return 0;

}

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

#define ll          long long int

#define pii         pair<int,int>

#define uu          first

#define vv          second

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

using namespace std;



const int MAX = 2e5 + 5;

const ll INF = 1e18;

int main()

{

    fastIO;

    int n, m;

    cin>>n>>m;

    

    vector< vector<int> >edg(n+1), rev(n+1);

    vector<int>deg(n+1), dp(n+1);

    

    while(m--) {

        

        int u , v;

        cin>>u>>v;

        edg[u].push_back(v);

        rev[v].push_back(u);

        deg[u]++;

    }

    

    queue<int>q;

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

        if(deg[i] == 0) q.push(i);

    }

    int ans = 0;

    

    while(!q.empty()) {

        

        int top = q.front();

        q.pop();

        for(auto x : edg[top]) dp[top] = max(dp[top] , dp[x] + 1);

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

        for(auto x : rev[top]) {

            deg[x]--;

            if(deg[x] == 0 ) q.push(x);

        }

    }

    cout<<ans<<'\n';

    return 0;

}