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

using namespace std;

#define mx 200005

vector<int>adj[mx];

bool vis[mx];

int dp[mx];



void dfs(int src){

vis[src] = 1;



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

    if(!vis[adj[src][i]])

        dfs(adj[src][i]);

dp[src] = max(dp[src],1 + dp[adj[src][i]]);

}

}

int longest_path(int n){

    int ans = 0;

    memset(dp,0,sizeof dp);

    memset(vis,0,sizeof vis);



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

        if(!vis[i])

            dfs(i);

    }

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

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

    }

return ans;

}









int main(){

int n,m;

cin>>n>>m;

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

    int x,y;

    cin>>x>>y;

    adj[x].push_back(y);

}



cout<<longest_path(n)<<endl;

}


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

using namespace std;



const int maxn=1e5+5;

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];

int dfs(int x)

{

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

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

	int ans=0;

	

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

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

	

	return dp[x]=ans;

}



int main()

{

	int n,m,x,y;

	memset(head,-1,sizeof(head));

	cin>>n>>m;

	while(m--)

	{

		cin>>x>>y;

		add(x,y);

		ru[y]++;

		chu[x]++;

	}

	

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

	{

		if(!ru[i])

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

	}

	cout<<ans<<endl;

	

	return 0;

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

using namespace std;

#define f first

#define s second

#define ll long long

const int N = 1e5+10;

vector < int > graph[N] ;

int vis[N] , n , mx1  ,m;

map < int , int > mp ;

int dfs ( int node ){

    int mx = 0 ;

    for (auto i : graph[node]){

        if (!vis[i]) {

            vis[i] = 1 ;

            mp[i] = dfs(i) ;

            mx = max(mx,mp[i]+1);

        }

        else

            mx = max(mx,1+mp[i]);

    }

    mp[node] = mx ;

    return mp[node] ;

}

int main()

{

    ios_base::sync_with_stdio( false ), cin.tie( nullptr ), cout.tie( nullptr );

    cin >> n >> m ;

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

        int u , v ;

        cin >> u >> v ;

        graph[u].push_back(v) ;

    }

    int mx = 0 ;

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

        if (!vis[i]){

            vis[i] = 1 ;

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

        }

    }

    cout << mx ;

}




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

using namespace std;



const int maxn=1e5+5;

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];

int dfs(int x)

{

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

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

	int ans=0;

	

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

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

	

	return dp[x]=ans;

}



int main()

{

	int n,m,x,y;

	memset(head,-1,sizeof(head));

	cin>>n>>m;

	while(m--)

	{

		cin>>x>>y;

		add(x,y);

		ru[y]++;

		chu[x]++;

	}

	

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

	{

		if(!ru[i])

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

	}

	cout<<ans<<endl;

	

	return 0;

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

#include <stdio.h>

#include <string>

#include <cstring>

#include <cstdio>

#include <algorithm>

#include <cmath>

#include <vector>

#include <set> 

#include <map>

#include <queue>

#include <deque>

#include <stack>

#include <bitset>

#include <iomanip>

using namespace std;

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

#define ll long long

#define ull unsigned long long

#define ld long double

#define ii pair<int, int>

#define pll pair<ll, ll>

#define vii vector<ii>

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

#define F first

#define S second

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

#define ReadFile freopen("input.txt","r",stdin)

#define WriteFile freopen("output.txt","w",stdout)

int x[] = { 0, -1, 0, 1, -1, 1, -1, 1 };

int y[] = { -1, 0, 1, 0, 1, -1, -1, 1 };

const int MOD = 998244353, N = 2e5 + 5;

const double pi = acos(-1);



vector<int> graph[N];

bool vis[N];

int dp[N];



void dfs(int node)

{

	if (vis[node]) return;

	vis[node] = true;

	for (int child : graph[node])

	{

		if(!vis[child])dfs(child);

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

	}

}



int main()

{

	assem99;



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

	f(i, 0, m)

	{

		int u, v; cin >> u >> v;

		graph[u].push_back(v);

	}

	int ans = 0;

	f(i, 1, n + 1) if (!vis[i]) dfs(i);

	f(i, 1, n + 1) ans = max(ans, dp[i]);



	cout << ans;



	return 0;

}


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

using namespace std;



const int maxn=1e5+5;

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];

int dfs(int x)

{

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

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

	int ans=0;

	

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

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

	

	return dp[x]=ans;

}



int main()

{

	int n,m,x,y;

	memset(head,-1,sizeof(head));

	cin>>n>>m;

	while(m--)

	{

		cin>>x>>y;

		add(x,y);

		ru[y]++;

		chu[x]++;

	}

	

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

	{

		if(!ru[i])

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

	}

	cout<<ans<<endl;

	

	return 0;

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

using namespace std;



int n, m, vis[100009], dis[100009];

vector<int> adj[100009];



int dfs(int s)

{

    if(vis[s])

        return dis[s];

    vis[s] = 1;

    int ret = 0;

    for(auto u : adj[s])

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



    return dis[s] = ret;

}



int main()

{

    int i;

    cin >> n >> m;



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

    {

        int x, y;

        cin >> x >> y;

        adj[x].push_back(y);

    }



    int ans = 0;

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

    {

        if(!vis[i])

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

    }



    cout << ans;

}


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

using namespace std;



const int maxn=1e5+5;

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];

int dfs(int x)

{

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

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

	int ans=0;

	

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

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

	

	return dp[x]=ans;

}



int main()

{

	int n,m,x,y;

	memset(head,-1,sizeof(head));

	cin>>n>>m;

	while(m--)

	{

		cin>>x>>y;

		add(x,y);

		ru[y]++;

		chu[x]++;

	}

	

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

	{

		if(!ru[i])

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

	}

	cout<<ans<<endl;

	

	return 0;

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



/**

       return NO_PAIN ? NO_GAIN : GAIN;

*/



using namespace std;



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

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

#define Unique(x) x.erase(unique(all(x)), x.end())

#define endl "\n"

#define pb push_back

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

#define isOdd(n)    (n&1)

#define pow(n, m) (ll)powl(n, m)

#define numOfDigits(n)  ((n)? (ll)(log10(n))+1 : 1)

#define clr(x, val)    memset(x, val, sizeof(x)) //be careful of value and TLE

#define MOD 1000000007

#define EPS 1e-7

#define LSOne(x)  (x & (-x))   //least significant 1

typedef long long ll;

typedef long double ld;

typedef pair<int, int> pii;

typedef vector<int> vi;



const int N = 1e5 + 1;

vector<vi> adj(N);

bool vis[N];

int dp[N];

void dfs(int u){

    /*

    for each vertex, ask it what is the longest path for you.

     then his parent's longest path is max + 1

    */

    vis[u] = 1;

    for(int i = 0; i<sz(adj[u]); i++){

        int v = adj[u][i];

        if(!vis[v])

            dfs(v);

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

    }

}

int main()

{

    START

    //freopen("test cases\\in.txt", "r", stdin) ;

    //freopen("test cases\\in.txt", "w", stdout);

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



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

        int u, v; cin>>u>>v;

        adj[u].pb(v);

    }

    int mx = 0;

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

        if(!vis[i] && sz(adj[i]))

            dfs(i);



    cout<<*max_element(dp, dp+N);

	return 0;

}


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

using namespace std;



const int maxn=1e5+5;

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];

int dfs(int x)

{

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

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

	int ans=0;

	

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

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

	

	return dp[x]=ans;

}



int main()

{

	int n,m,x,y;

	memset(head,-1,sizeof(head));

	cin>>n>>m;

	while(m--)

	{

		cin>>x>>y;

		add(x,y);

		ru[y]++;

		chu[x]++;

	}

	

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

	{

		if(!ru[i])

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

	}

	cout<<ans<<endl;

	

	return 0;

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

using namespace std;



int dp[100005];



vector<int>adj[100005];



int dfs(int x){

    if (dp[x] != 0) return dp[x];

    int res = 0;

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

        res = max(dfs(adj[x][i]),res);

    }

    return dp[x] = 1+res;

}



int main(int argc, char const *argv[]) {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

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

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

        int x,y; cin >> x >> y;

        adj[x].push_back(y);

    }

    int res = 0;

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

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

    }

    cout << res-1 << endl;

    return 0;

}


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

using namespace std;



const int maxn=1e5+5;

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];

int dfs(int x)

{

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

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

	int ans=0;

	

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

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

	

	return dp[x]=ans;

}



int main()

{

	int n,m,x,y;

	memset(head,-1,sizeof(head));

	cin>>n>>m;

	while(m--)

	{

		cin>>x>>y;

		add(x,y);

		ru[y]++;

		chu[x]++;

	}

	

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

	{

		if(!ru[i])

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

	}

	cout<<ans<<endl;

	

	return 0;

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

using namespace std ;

#define ll long long



inline int D(){

    int t ;

    scanf( "%d" , &t ) ;

    return t ;

}

inline ll LLD(){

    ll t ;

    scanf( "%lld" , &t ) ;

    return t ;

}

//const int INF = 0x3f3f3f3f;

//const int mod=1e9+7;

//###################################################################

const int N=100009;

int n,m;

vector<vector<int>>g(N);



int mem[N];

int solve(int num){

    if(mem[num]!=-1)return mem[num];

    if(g[num].size()) {

        int ret=0;

        for (auto i:g[num]) {

            ret =max(ret,solve(i));

        }

        return mem[num]=1+ret;

    }

    return mem[num]=0;



}



int main(){

    n=D(),m=D();

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

        int u=D(),v=D();

        g[u].push_back(v);

    }

    int mx=0;

    memset(mem,-1,sizeof(mem));

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

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

    }

    printf("%d",mx);

}

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

using namespace std;



const int maxn=1e5+5;

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];

int dfs(int x)

{

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

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

	int ans=0;

	

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

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

	

	return dp[x]=ans;

}



int main()

{

	int n,m,x,y;

	memset(head,-1,sizeof(head));

	cin>>n>>m;

	while(m--)

	{

		cin>>x>>y;

		add(x,y);

		ru[y]++;

		chu[x]++;

	}

	

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

	{

		if(!ru[i])

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

	}

	cout<<ans<<endl;

	

	return 0;

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

using namespace std;



vector <int> dp[100010], ed[100010];



int solve(int i, int j){

	

	if(j == (int)ed[i].size()) return 0;

	

	if(dp[i][j] == -1) dp[i][j] = max(solve(ed[i][j], 0) + 1, solve(i, j + 1));

	

	return dp[i][j];

}

	



int main(){

	

	int n, m, ans = 0;

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

	

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

		int a, b;

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

		

		ed[a].push_back(b);

		dp[a].push_back(-1);

	}

	

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

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

			if(dp[i][j] == -1) solve(i, j);

			

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

		}

	}

	

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

	

	return 0;

}


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

using namespace std;



const int maxn=1e5+5;

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];

int dfs(int x)

{

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

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

	int ans=0;

	

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

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

	

	return dp[x]=ans;

}



int main()

{

	int n,m,x,y;

	memset(head,-1,sizeof(head));

	cin>>n>>m;

	while(m--)

	{

		cin>>x>>y;

		add(x,y);

		ru[y]++;

		chu[x]++;

	}

	

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

	{

		if(!ru[i])

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

	}

	cout<<ans<<endl;

	

	return 0;

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

using namespace std;



int N, M, a, b, ans;

vector<int> V[100100];

int dp[100100];



int f(int a) {

	if (V[a].size() == 0) return 0;

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

	dp[a] = 0;

	for(int i=0; i<V[a].size(); i++) {

		int b = V[a][i];

		dp[a] = max(dp[a], f(b)+1);

	}

	return dp[a];

}



int main() {

	cin >> N >> M;

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

		cin >> a >> b;

		V[a].push_back(b);

	}

	memset(dp, -1, sizeof(dp));

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

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

	}

	cout << ans << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1164751963875026x):
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<vector>

#include<queue>

using namespace std;

const int maxn = 1e5 + 5;

vector<int>G[maxn];

queue<int>q;

int in[maxn];

int main(){

	int  n,m;

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

	int x,y;

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

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

		G[x].push_back(y);

		in[y]++;

	}

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

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

	}

	int ans = 0;

	while(!q.empty()){

		int cnt = q.size();

		//cout << cnt << endl;

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

		int cur = q.front();

		q.pop();

		int len = G[cur].size();

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

			int tmp = G[cur][i];

			in[tmp]--;

			if(in[tmp] == 0) q.push(tmp);

		}

		}

		ans++;

	}

	ans--;

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

	return 0;

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

using namespace std;

#define N (int)2e5

#define fi first

#define se second

#define mp make_pair

#define pb push_back

int n,m,memo[N];

vector <int> adj[N];



int dp(int u) {

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

    int &sol = memo[u];

    if (sol != -1) return sol;

    int keep = INT_MIN;

    for (int v : adj[u]) {

        keep = max(

            keep ,

            dp(v) + 1

        );

    }

    return sol = keep;

}



int main() {

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

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

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

        adj[u].pb(v);

    }

    memset (memo , -1 , sizeof memo);

    int sol = INT_MIN;

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

        sol = max(sol , dp(u));

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

}


// Faster C++ solution for the same problem (measured speedup: 2.092097725839503x):
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<vector>

#include<queue>

using namespace std;

const int maxn = 1e5 + 5;

vector<int>G[maxn];

queue<int>q;

int in[maxn];

int main(){

	int  n,m;

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

	int x,y;

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

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

		G[x].push_back(y);

		in[y]++;

	}

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

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

	}

	int ans = 0;

	while(!q.empty()){

		int cnt = q.size();

		//cout << cnt << endl;

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

		int cur = q.front();

		q.pop();

		int len = G[cur].size();

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

			int tmp = G[cur][i];

			in[tmp]--;

			if(in[tmp] == 0) q.push(tmp);

		}

		}

		ans++;

	}

	ans--;

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

	return 0;

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

using namespace std;

typedef long long ll;

#define fast ios_base::sync_with_stdio(false),cin.tie(NULL)

#define INF 1e18

int n, m;

vector<int> graph[100020];

vector<int> dp(100020, -1);

vector<bool> visited(100020, false);

int dfs(int node, int sum)

{

    if(dp[node] != -1)

        return dp[node];

    if(graph[node].empty())

        return 1;

    else{

        int temp, max = 0;

        for (auto child : graph[node]) {

            temp = dfs(child, sum+1);

            if(temp > max)

                max = temp;

        }

        return dp[node] = max+1;

    }

}

int check()

{

    int mx = 0;

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

        if(!visited[i])

            mx = max(mx, dfs(i, 0));

    }

    return mx-1;

}

int main() {

    fast;

    cin>>n>>m;

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

        int a, b;

        cin>>a>>b;

        graph[a].push_back(b);

    }

    cout<<check();

}

// Faster C++ solution for the same problem (measured speedup: 1.127841997702295x):
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<vector>

#include<queue>

using namespace std;

const int maxn = 1e5 + 5;

vector<int>G[maxn];

queue<int>q;

int in[maxn];

int main(){

	int  n,m;

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

	int x,y;

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

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

		G[x].push_back(y);

		in[y]++;

	}

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

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

	}

	int ans = 0;

	while(!q.empty()){

		int cnt = q.size();

		//cout << cnt << endl;

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

		int cur = q.front();

		q.pop();

		int len = G[cur].size();

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

			int tmp = G[cur][i];

			in[tmp]--;

			if(in[tmp] == 0) q.push(tmp);

		}

		}

		ans++;

	}

	ans--;

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

	return 0;

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

using namespace std;

#define mx 200005

vector<int>adj[mx];

bool vis[mx];

int dp[mx];



void dfs(int src){

vis[src] = 1;



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

    if(!vis[adj[src][i]])

        dfs(adj[src][i]);

dp[src] = max(dp[src],1 + dp[adj[src][i]]);

}

}

int longest_path(int n){

    int ans = 0;

    memset(dp,0,sizeof dp);

    memset(vis,0,sizeof vis);



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

        if(!vis[i])

            dfs(i);

    }

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

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

    }

return ans;

}









int main(){

int n,m;

cin>>n>>m;

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

    int x,y;

    cin>>x>>y;

    adj[x].push_back(y);

}



cout<<longest_path(n)<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 2.153356286261222x):
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<vector>

#include<queue>

using namespace std;

const int maxn = 1e5 + 5;

vector<int>G[maxn];

queue<int>q;

int in[maxn];

int main(){

	int  n,m;

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

	int x,y;

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

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

		G[x].push_back(y);

		in[y]++;

	}

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

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

	}

	int ans = 0;

	while(!q.empty()){

		int cnt = q.size();

		//cout << cnt << endl;

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

		int cur = q.front();

		q.pop();

		int len = G[cur].size();

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

			int tmp = G[cur][i];

			in[tmp]--;

			if(in[tmp] == 0) q.push(tmp);

		}

		}

		ans++;

	}

	ans--;

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

	return 0;

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

#include <stdio.h>

#include <string>

#include <cstring>

#include <cstdio>

#include <algorithm>

#include <cmath>

#include <vector>

#include <set> 

#include <map>

#include <queue>

#include <deque>

#include <stack>

#include <bitset>

#include <iomanip>

using namespace std;

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

#define ll long long

#define ull unsigned long long

#define ld long double

#define ii pair<int, int>

#define pll pair<ll, ll>

#define vii vector<ii>

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

#define F first

#define S second

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

#define ReadFile freopen("input.txt","r",stdin)

#define WriteFile freopen("output.txt","w",stdout)

int x[] = { 0, -1, 0, 1, -1, 1, -1, 1 };

int y[] = { -1, 0, 1, 0, 1, -1, -1, 1 };

const int MOD = 998244353, N = 2e5 + 5;

const double pi = acos(-1);



vector<int> graph[N];

bool vis[N];

int dp[N];



void dfs(int node)

{

	if (vis[node]) return;

	vis[node] = true;

	for (int child : graph[node])

	{

		if(!vis[child])dfs(child);

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

	}

}



int main()

{

	assem99;



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

	f(i, 0, m)

	{

		int u, v; cin >> u >> v;

		graph[u].push_back(v);

	}

	int ans = 0;

	f(i, 1, n + 1) if (!vis[i]) dfs(i);

	f(i, 1, n + 1) ans = max(ans, dp[i]);



	cout << ans;



	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.8417319735599955x):
#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<vector>

#include<queue>

using namespace std;

const int maxn = 1e5 + 5;

vector<int>G[maxn];

queue<int>q;

int in[maxn];

int main(){

	int  n,m;

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

	int x,y;

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

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

		G[x].push_back(y);

		in[y]++;

	}

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

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

	}

	int ans = 0;

	while(!q.empty()){

		int cnt = q.size();

		//cout << cnt << endl;

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

		int cur = q.front();

		q.pop();

		int len = G[cur].size();

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

			int tmp = G[cur][i];

			in[tmp]--;

			if(in[tmp] == 0) q.push(tmp);

		}

		}

		ans++;

	}

	ans--;

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

	return 0;

}