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: 1.1892398854747948x):
#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;

}
// 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: 2.476894756769245x):
#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;

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: 2.534137746267947x):
#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:
/* 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: 2.5547901963172226x):
#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: 2.8233201276233038x):
#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 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: 5.2904548506132185x):
#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<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: 2.476656317447462x):
#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<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: 2.5288706418482434x):
#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:
//#pragma GCC target ("avx2,sse2")

//#pragma GCC optimization ("Ofast")

//#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



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

#define ll long long

#define ull unsigned long long

#define db long double

#define pb push_back

#define pf push_front

#define ppb pop_back

#define ppf pop_front

#define fi first

#define se second

#define mp make_pair

#define up_b upper_bound

#define low_b lower_bound

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

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

#define endl "\n"



#define left fsdsdfoisf

#define sum dpsdfioppsf

#define assign xcvjlkdjfio

#define trie fksdfkjkfnjuiv

#define next sidlfjsfkl

#define merge sdfksdkfsldf



//#define int long long



using namespace std;



void dout() {

    cerr << endl;

}

template <typename Head, typename... Tail>

void dout(Head H, Tail... T) {

    cerr << H << ' ';

    dout(T...);

}



//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef pair <int, int> pii;



const int N = 1e5 + 7;

int n, m, dp[N];

vector <int> q[N], r[N], ord;

bool used[N];



void dfs(int v) {

    used[v] = true;

    for (auto i : q[v]) {

        if (!used[i]) {

            dfs(i);

        }

    }

    ord.pb(v);

}



void solve(int tc) {

    // check for (int i = 0; i < n; j++)

    cin >> n >> m;

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

        int x, y;

        cin >> x >> y;

        q[x].pb(y);

        r[y].pb(x);

    }

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

        if (!used[i]) {

            dfs(i);

        }

    }

    reverse(all(ord));

    int res = 0;

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

        int v = ord[i - 1];

        for (auto j : r[v]) {

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

        }

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

    }

    cout << res;

}



signed main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

//    freopen("input.txt", "r", stdin);

//    freopen("output.txt", "w", stdout);

    int tc = 1;

//    cin >> tc;

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

        solve(i);

//        cleanup();

    }

}


// Faster C++ solution for the same problem (measured speedup: 4.609827958192755x):
#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 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;

}




// Faster C++ solution for the same problem (measured speedup: 2.154236209015896x):
#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>

#define ll long long

#define ull unsigned long long

#define IO ios::sync_with_stdio(false), cin.tie(0)

#define endl '\n'

#define main() int main()

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

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

#define pi acos(-1.0)

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

#define lowbit(x) ((x)&(-(x)))

#define pb push_back

#define binarycount __builtin_popcount(x)

#define tr t[root]

#define lson t[root << 1]

#define rson t[root << 1 | 1]

#define fir first

#define sec second

#define debug(x) cout<<x<<'\n'

#define sa(a) scanf("%d", &a)

#define sab(a, b) scanf("%d%d", &a, &b)

#define pa(a) printf("%d",a)

#define pab(a,b) printf("%d%d",a,b)

#define pii pair<string,int>

using namespace std;

#define maxn 100005

const int inf = 0x3f3f3f3f;

const int mod = 9973;

const double eps = 1e-8;

ll gcd(ll a, ll b) {return b==0?a:gcd(b, a%b);}

ll lcm(ll a, ll b) {return a/gcd(a, b)*b;}

int max(int x,int y){if(x>y) return x;return y;}

ll powmod(ll a,ll b){

    ull ans = 1, pow = a%mod;

    while(b) { if(b&1) ans = (ans*pow)%mod; pow = (pow*pow)%mod; b >>= 1; }

    return ans%mod;

}



struct Edge

{

    int to, next, val;

} e[maxn << 1];

int head[maxn], tot = 0;

int dis[maxn], dep[maxn], pre[maxn][21];

int n,m,ans;

int d[maxn],c[maxn],dp[maxn];

void init()

{

    ms(head, -1);

    tot = 0;ms(d,0);ans=0;ms(dp,0);

}

void add(int x, int y, int val)

{

    e[tot].to = y, e[tot].val = val, e[tot].next = head[x], head[x] = tot++;

}



int dfs(int x)

{

	if(!c[x]) return 0;

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

	ll tt=0;

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

    {

    	int y=e[i].to;

        tt=max(tt,dfs(y)+1);

    }

    return dp[x]=tt;

}

main()

{

	cin>>n>>m;

	init();

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

	{

		int x,y;

		cin>>x>>y;

		add(x,y,1);

		if(!d[y])d[y]=1;//d为出度 

		if(!c[x])c[x]=1;

	}

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

	{

		if(!d[i]){

			dfs(i);

		}

	}

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

	{

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

		//cout<<dp[i]<<endl;

	}

	cout<<ans<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.5807963121702047x):
#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;

vector<int> edges[100100];

bool vis[100100];

int dp[100100];



int dfs( int node ) {

  if ( vis[node] ) return dp[node];

  vis[node] = 1;

  int res = 0;

  for (auto el: edges[node])

    res = max(res, 1 + dfs(el));

  return dp[node] = res;

}



int main() {

  ios::sync_with_stdio(false);

  cin >> n >> m;



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

    int u, v;

    cin >> u >> v;

    edges[u].push_back(v);

  }



  int ans = 0;

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

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

  }



  cout << ans << endl;

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.547822612474777x):
#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;

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: 2.852064267253621x):
#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;

 }