stablegradients/pie-gem5-pairs huggingface dataset:
// 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.139972513640135x):
#include<queue>

#include<cstdio>

using namespace std;

const int maxn = 1e5 + 5;



queue<int>que;

vector<int>vec[maxn];

int in[maxn],ans[maxn];



int main(){

	int n, m;

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

	int u,v;

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

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

		vec[u].push_back(v);

		in[v]++;

	}

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

		if(in[i] == 0){

			que.push(i);

			ans[i] = 0;

		}

	}

	int now;

	while(!que.empty()){

		now = que.front();que.pop();

		int len = vec[now].size();

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

			in[vec[now][i]]--;

			if(in[vec[now][i]] == 0){

				que.push(vec[now][i]);

				ans[vec[now][i]] = ans[now] + 1;

			}

		}

	}

	int cnt = 0;

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

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

}
// 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.1361279776056157x):
#include<queue>

#include<cstdio>

using namespace std;

const int maxn = 1e5 + 5;



queue<int>que;

vector<int>vec[maxn];

int in[maxn],ans[maxn];



int main(){

	int n, m;

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

	int u,v;

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

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

		vec[u].push_back(v);

		in[v]++;

	}

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

		if(in[i] == 0){

			que.push(i);

			ans[i] = 0;

		}

	}

	int now;

	while(!que.empty()){

		now = que.front();que.pop();

		int len = vec[now].size();

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

			in[vec[now][i]]--;

			if(in[vec[now][i]] == 0){

				que.push(vec[now][i]);

				ans[vec[now][i]] = ans[now] + 1;

			}

		}

	}

	int cnt = 0;

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

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

}
// 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.1164351710611262x):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<queue>

#include<cstring>

#include<vector>

#define inf 0x3f3f3f3f

using namespace std;

typedef long long LL;

const int maxn = 1e5+5;



vector<int>vec[maxn];

queue<int>q;

int in[maxn],num[maxn];



int main(){	

	int n,m;

	scanf("%d",&n);

	scanf("%d",&m);

	for(int i = 0; i <= n; i++) in[i] = 0;

	int u,v;

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

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

		vec[u].push_back(v);

		in[v]++;

	}

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

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

	}

	int ans = 0;

	while(!q.empty()){

		int now = q.front(); q.pop();

		int len = vec[now].size();

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

			if(--in[vec[now][i]] == 0){

				q.push(vec[now][i]);

				num[vec[now][i]] = num[now] + 1;

				ans = max(ans, num[vec[now][i]]);

			}

		}

	}

	printf("%d",ans);

}
// 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.0920227247158247x):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<queue>

#include<cstring>

#include<vector>

#define inf 0x3f3f3f3f

using namespace std;

typedef long long LL;

const int maxn = 1e5+5;



vector<int>vec[maxn];

queue<int>q;

int in[maxn],num[maxn];



int main(){	

	int n,m;

	scanf("%d",&n);

	scanf("%d",&m);

	for(int i = 0; i <= n; i++) in[i] = 0;

	int u,v;

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

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

		vec[u].push_back(v);

		in[v]++;

	}

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

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

	}

	int ans = 0;

	while(!q.empty()){

		int now = q.front(); q.pop();

		int len = vec[now].size();

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

			if(--in[vec[now][i]] == 0){

				q.push(vec[now][i]);

				num[vec[now][i]] = num[now] + 1;

				ans = max(ans, num[vec[now][i]]);

			}

		}

	}

	printf("%d",ans);

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

#include<queue>

#include<vector>

using namespace std;

const int N=1e5+10;

vector<int>f[N],a;

queue<int>q;

int n,m,p,x,y,in[N],ans,dp[N];

int main()

{

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

	while(m--)

	{

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

		f[x].push_back(y);

		in[y]++;

	}

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

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

	while(!q.empty())

	{

		p=q.front();

		q.pop();

		a.push_back(p);

		for(int j=0;j<f[p].size();j++)

			if(!--in[f[p][j]])q.push(f[p][j]);

	}

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

	{

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

			dp[f[a[i]][j]]=dp[a[i]]+1;

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

	}

	printf("%d",ans);

	return 0;

}

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



using namespace std;



// Header files, namespaces,

// macros as defined above

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>





#define f first

//#define s second

#define db long double

#define pb push_back

#define sz(c) (c).size()

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



typedef long long ll;

typedef pair <int , int> ii;

typedef pair <ii , int> iii;

typedef vector <int> vi;

typedef vector <ii> vii;

typedef vector <vi> vvi;

typedef vector <vii> vvii;



const ll mod = 998244353;

const int MAX = 1e5+5;

const db eps = 1e-13;



bool mk[MAX];

int dp[MAX];



vvi g;



int dfs_dp( int node ) {



    mk[node] = true;

    int mx = 0;

    for( int nw : g[node] ) {



        if( !mk[nw] )

            mx = max( dfs_dp(nw) , mx );

        else

            mx = max( dp[nw] , mx );

    }



    return dp[node] = mx+1;

}







int main()

{

	ios_base::sync_with_stdio(0);

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

    cout<<setprecision(0)<<fixed;

    int ans = 0;

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

    vi v;

    g = vvi(n+1,v);

    while(m--) {



        int a, b;

        cin>>a>>b;

        g[a].pb(b);



    }



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

        if( !mk[i] )

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



    cout<<ans-1<<"\n";



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

using namespace std;

const int maxn=100010;

int n,m,ans;

vector<int> edge[maxn];

int in_dgr[maxn],dp[maxn];

queue<int> q;

int main()

{

	cin>>n>>m;

	while(m--)

	{

		int u,v;

		cin>>u>>v;

		edge[u].push_back(v);

		in_dgr[v]++;

	}

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

		if(!in_dgr[i])

			q.push(i);

	while(!q.empty())

	{

		int now=q.front();

		q.pop();

		for(int i=0;i<edge[now].size();i++)

		{

			int to=edge[now][i];

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

			if(--in_dgr[to]==0)

				q.push(to);

			ans=dp[to];

		}

	}

	printf("%d",ans);

	return 0;

}

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



using namespace std;



// Header files, namespaces,

// macros as defined above

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>





#define f first

//#define s second

#define db long double

#define pb push_back

#define sz(c) (c).size()

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



typedef long long ll;

typedef pair <int , int> ii;

typedef pair <ii , int> iii;

typedef vector <int> vi;

typedef vector <ii> vii;

typedef vector <vi> vvi;

typedef vector <vii> vvii;



const ll mod = 998244353;

const int MAX = 1e5+5;

const db eps = 1e-13;



bool mk[MAX];

int dp[MAX];



vvi g;



int dfs_dp( int node ) {



    mk[node] = true;

    int mx = 0;

    for( int nw : g[node] ) {



        if( !mk[nw] )

            mx = max( dfs_dp(nw) , mx );

        else

            mx = max( dp[nw] , mx );

    }



    return dp[node] = mx+1;

}







int main()

{

	ios_base::sync_with_stdio(0);

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

    cout<<setprecision(0)<<fixed;

    int ans = 0;

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

    vi v;

    g = vvi(n+1,v);

    while(m--) {



        int a, b;

        cin>>a>>b;

        g[a].pb(b);



    }



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

        if( !mk[i] )

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



    cout<<ans-1<<"\n";



}
// Slower C++ solution:
/* long long or int */

/* inline & usighed & sighned main */

/* xx 396 */



#include<bits/stdc++.h>

using namespace std;



const int MAX_N=1e5+10;



stack <int> S;

int N,M,deg[MAX_N],dist[MAX_N];

vector <int> G[MAX_N];



signed main(){

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

	cin>>N>>M;

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

		int X,Y; cin>>X>>Y; X--; Y--;

		G[X].push_back(Y); deg[Y]++;

	}

	for(int i=0;i<N;i++) if(deg[i]==0) S.push(i);

	while(!S.empty()){

		int fr=S.top(); S.pop();

		for(int i=0;i<G[fr].size();i++){

			int to=G[fr][i]; deg[to]--;

			dist[to]=max(dist[to],dist[fr]+1);

			if(deg[to]==0) S.push(to);

		}

	}

	cout<<(*max_element(dist,dist+N))<<endl;

	return 0;

}

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



using namespace std;



// Header files, namespaces,

// macros as defined above

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>





#define f first

//#define s second

#define db long double

#define pb push_back

#define sz(c) (c).size()

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



typedef long long ll;

typedef pair <int , int> ii;

typedef pair <ii , int> iii;

typedef vector <int> vi;

typedef vector <ii> vii;

typedef vector <vi> vvi;

typedef vector <vii> vvii;



const ll mod = 998244353;

const int MAX = 1e5+5;

const db eps = 1e-13;



bool mk[MAX];

int dp[MAX];



vvi g;



int dfs_dp( int node ) {



    mk[node] = true;

    int mx = 0;

    for( int nw : g[node] ) {



        if( !mk[nw] )

            mx = max( dfs_dp(nw) , mx );

        else

            mx = max( dp[nw] , mx );

    }



    return dp[node] = mx+1;

}







int main()

{

	ios_base::sync_with_stdio(0);

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

    cout<<setprecision(0)<<fixed;

    int ans = 0;

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

    vi v;

    g = vvi(n+1,v);

    while(m--) {



        int a, b;

        cin>>a>>b;

        g[a].pb(b);



    }



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

        if( !mk[i] )

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



    cout<<ans-1<<"\n";



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



using namespace std;



// Header files, namespaces,

// macros as defined above

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>





#define f first

//#define s second

#define db long double

#define pb push_back

#define sz(c) (c).size()

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



typedef long long ll;

typedef pair <int , int> ii;

typedef pair <ii , int> iii;

typedef vector <int> vi;

typedef vector <ii> vii;

typedef vector <vi> vvi;

typedef vector <vii> vvii;



const ll mod = 998244353;

const int MAX = 1e5+5;

const db eps = 1e-13;



bool mk[MAX];

int dp[MAX];



vvi g;



int dfs_dp( int node ) {



    mk[node] = true;

    int mx = 0;

    for( int nw : g[node] ) {



        if( !mk[nw] )

            mx = max( dfs_dp(nw) , mx );

        else

            mx = max( dp[nw] , mx );

    }



    return dp[node] = mx+1;

}







int main()

{

	ios_base::sync_with_stdio(0);

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

    cout<<setprecision(0)<<fixed;

    int ans = 0;

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

    vi v;

    g = vvi(n+1,v);

    while(m--) {



        int a, b;

        cin>>a>>b;

        g[a].pb(b);



    }



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

        if( !mk[i] )

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



    cout<<ans-1<<"\n";



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



using namespace std;



// Header files, namespaces,

// macros as defined above

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>





#define f first

//#define s second

#define db long double

#define pb push_back

#define sz(c) (c).size()

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



typedef long long ll;

typedef pair <int , int> ii;

typedef pair <ii , int> iii;

typedef vector <int> vi;

typedef vector <ii> vii;

typedef vector <vi> vvi;

typedef vector <vii> vvii;



const ll mod = 998244353;

const int MAX = 1e5+5;

const db eps = 1e-13;



bool mk[MAX];

int dp[MAX];



vvi g;



int dfs_dp( int node ) {



    mk[node] = true;

    int mx = 0;

    for( int nw : g[node] ) {



        if( !mk[nw] )

            mx = max( dfs_dp(nw) , mx );

        else

            mx = max( dp[nw] , mx );

    }



    return dp[node] = mx+1;

}







int main()

{

	ios_base::sync_with_stdio(0);

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

    cout<<setprecision(0)<<fixed;

    int ans = 0;

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

    vi v;

    g = vvi(n+1,v);

    while(m--) {



        int a, b;

        cin>>a>>b;

        g[a].pb(b);



    }



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

        if( !mk[i] )

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



    cout<<ans-1<<"\n";



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

#include<cstdio>

using namespace std;

const int maxn = 1e5 + 5;



queue<int>que;

vector<int>vec[maxn];

int in[maxn],ans[maxn];



int main(){

	int n, m;

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

	int u,v;

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

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

		vec[u].push_back(v);

		in[v]++;

	}

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

		if(in[i] == 0){

			que.push(i);

			ans[i] = 0;

		}

	}

	int now;

	while(!que.empty()){

		now = que.front();que.pop();

		int len = vec[now].size();

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

			in[vec[now][i]]--;

			if(in[vec[now][i]] == 0){

				que.push(vec[now][i]);

				ans[vec[now][i]] = ans[now] + 1;

			}

		}

	}

	int cnt = 0;

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

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

}

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



using namespace std;



// Header files, namespaces,

// macros as defined above

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>





#define f first

//#define s second

#define db long double

#define pb push_back

#define sz(c) (c).size()

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



typedef long long ll;

typedef pair <int , int> ii;

typedef pair <ii , int> iii;

typedef vector <int> vi;

typedef vector <ii> vii;

typedef vector <vi> vvi;

typedef vector <vii> vvii;



const ll mod = 998244353;

const int MAX = 1e5+5;

const db eps = 1e-13;



bool mk[MAX];

int dp[MAX];



vvi g;



int dfs_dp( int node ) {



    mk[node] = true;

    int mx = 0;

    for( int nw : g[node] ) {



        if( !mk[nw] )

            mx = max( dfs_dp(nw) , mx );

        else

            mx = max( dp[nw] , mx );

    }



    return dp[node] = mx+1;

}







int main()

{

	ios_base::sync_with_stdio(0);

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

    cout<<setprecision(0)<<fixed;

    int ans = 0;

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

    vi v;

    g = vvi(n+1,v);

    while(m--) {



        int a, b;

        cin>>a>>b;

        g[a].pb(b);



    }



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

        if( !mk[i] )

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



    cout<<ans-1<<"\n";



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

#include<iostream>

#include<algorithm>

#include<queue>

#include<cstring>

#include<vector>

#define inf 0x3f3f3f3f

using namespace std;

typedef long long LL;

const int maxn = 1e5+5;



vector<int>vec[maxn];

queue<int>q;

int in[maxn],num[maxn];



int main(){	

	int n,m;

	scanf("%d",&n);

	scanf("%d",&m);

	for(int i = 0; i <= n; i++) in[i] = 0;

	int u,v;

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

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

		vec[u].push_back(v);

		in[v]++;

	}

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

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

	}

	int ans = 0;

	while(!q.empty()){

		int now = q.front(); q.pop();

		int len = vec[now].size();

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

			if(--in[vec[now][i]] == 0){

				q.push(vec[now][i]);

				num[vec[now][i]] = num[now] + 1;

				ans = max(ans, num[vec[now][i]]);

			}

		}

	}

	printf("%d",ans);

}

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



using namespace std;



// Header files, namespaces,

// macros as defined above

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>





#define f first

//#define s second

#define db long double

#define pb push_back

#define sz(c) (c).size()

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



typedef long long ll;

typedef pair <int , int> ii;

typedef pair <ii , int> iii;

typedef vector <int> vi;

typedef vector <ii> vii;

typedef vector <vi> vvi;

typedef vector <vii> vvii;



const ll mod = 998244353;

const int MAX = 1e5+5;

const db eps = 1e-13;



bool mk[MAX];

int dp[MAX];



vvi g;



int dfs_dp( int node ) {



    mk[node] = true;

    int mx = 0;

    for( int nw : g[node] ) {



        if( !mk[nw] )

            mx = max( dfs_dp(nw) , mx );

        else

            mx = max( dp[nw] , mx );

    }



    return dp[node] = mx+1;

}







int main()

{

	ios_base::sync_with_stdio(0);

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

    cout<<setprecision(0)<<fixed;

    int ans = 0;

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

    vi v;

    g = vvi(n+1,v);

    while(m--) {



        int a, b;

        cin>>a>>b;

        g[a].pb(b);



    }



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

        if( !mk[i] )

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



    cout<<ans-1<<"\n";



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

#include<queue>

#include<vector>

using namespace std;

const int N=1e5+10;

vector<int>f[N],a;

queue<int>q;

int n,m,p,x,y,in[N],ans,dp[N];

int main()

{

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

	while(m--)

	{

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

		f[x].push_back(y);

		in[y]++;

	}

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

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

	while(!q.empty())

	{

		p=q.front();

		q.pop();

		a.push_back(p);

		for(int j=0;j<f[p].size();j++)

			if(!--in[f[p][j]])q.push(f[p][j]);

	}

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

	{

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

			dp[f[a[i]][j]]=dp[a[i]]+1;

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

	}

	printf("%d",ans);

	return 0;

}

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



using namespace std;



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

#define clr(v, d)       memset(v, d, sizeof(v))

#define rep(i, v)       for(int i=0; i<sz(v); ++i)

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

#define lpi(i, j, n)    for(int i=(j); i<(int)(n); ++i)

#define lpd(i, j, n)    for(int i=(j); i>=(int)(n); --i)

#define INF             1000000000





typedef long long ll;

typedef pair<int, int> ii;

typedef vector<ii> vii;

typedef vector<int> vi;



struct edge{

	int from;

	int to;

	int nxt_edge;

};



const int N = 1e5+5;



int n, e;

struct edge edges[N];

int heads[N];

int dp[N];



int solve(int cur_edge_idx){

	if(cur_edge_idx == -1)

		return 0;

// 	cout << cur_edge_idx << endl;

	int & cur = dp[cur_edge_idx];

	if(~cur)

		return cur;

	cur  = solve(edges[cur_edge_idx].nxt_edge);

	if(edges[cur_edge_idx].to > 0)

	    cur = max(cur, 1 + solve(heads[edges[cur_edge_idx].to]));

	return cur;

}



int main()

{

    cin >> n >> e;

	clr(edges, 0);

	clr(heads, -1);

	clr(dp, -1);

    lp(i, e){

    	cin >> edges[i].from >> edges[i].to;

    	edges[i].nxt_edge = heads[edges[i].from];

    	heads[edges[i].from] = i;

    }

    int res = 0;

    lpi(i, 1, n+1)

    	res = max(res, solve(heads[i]));

    cout << res << endl;

    return 0;

}