stablegradients/pie-gem5-pairs huggingface dataset:
// 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: 3.495902094865645x):
#include<bits/stdc++.h>

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

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

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

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

using namespace std;

#define ll long long

#define ull unsigned long long

#define mst(a, b) memset((a), (b), sizeof(a))

#define mp(a, b) make_pair(a, b)

#define pi acos(-1)

#define pii pair<int, int>

#define pb push_back

const int INF = 0x3f3f3f3f;

const double eps = 1e-10;

const int maxn = 1e5 + 7;

const int maxm = 5e5 + 10;

const int mod = 1e9 + 7;

vector<int> s[maxn];

int vis[maxn];

int dp[maxn];

int main()

{

    ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    mst(vis, 0);

    mst(dp, 0);

    queue<int> q;

    int n, m;

    cin >> n >> m;

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

    {

        int x, y;

        cin >> x >> y;

        vis[y]++;

        s[x].pb(y);

    }

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

    {

        if (!vis[i])

            q.push(i);

    }

    while (!q.empty())

    {

        int now = q.front();

        q.pop();

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

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

        {

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

            vis[s[now][i]]--;

            if (vis[s[now][i]] == 0)

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

        }

    }

    int ans = 0;

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

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

    cout << ans << endl;

    return 0;

}

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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

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

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

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

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

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

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

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



using namespace std;



#define el				'\n'

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

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

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



double const EPS = 1e-8, PI = acos(-1);

const int N = 1e5 + 9, M = 50 + 9, OO = (int) 1e9 + 1;

const long long MOD = 1e9 + 7;



typedef long long ll;



void OUTPUT() {

	cout << fixed << setprecision(12);

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

}



void INPUT() {

#ifndef ONLINE_JUDGE

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

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

#endif

	cout << fixed << setprecision(0);

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

}



int n, m, x, y, vis[N], ans;

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



int DFS(int node) {

	if (vis[node] != -1)

		return vis[node];

	vis[node] = 0;

	for (auto child : edges[node]) {

		DFS(child);

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

	}

	return 0;

}



int main() {

//	INPUT();

	OUTPUT();

	clr(vis, -1);

	cin >> n >> m;

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

		cin >> x >> y;

		edges[x].push_back(y);

	}

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

		DFS(j);

	}

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

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

	}

	cout << ans;



	return 0;



}


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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

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

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

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

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

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

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

}
// Slower C++ solution:
/*

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

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

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

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

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

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

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;

}

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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

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

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

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

using namespace std;

#define ll long long

#define ull unsigned long long

#define PII pair<int,int>

#define PSI pair<string,int>

#define PIII pair< int, pair<int,int> >

const int MaxN = 1e5+5;

vector<int> g[MaxN];

int dp[MaxN];

int N;



int dfs(int node, int pr){



    auto &ret = dp[node];

    if(ret!=-1) return ret;

    ret = 0;

    for(auto i: g[node]){

        if(i==pr) continue;

        ret = max( ret, dfs(i,node)+1 );

    }



    return ret;

}



void read(){



    int i,edge;

    memset(dp,-1,sizeof dp);

    cin>>N>>edge;

    for(i=0; i<edge; i++){

        int u,v;

        cin>>u>>v;

        g[u].push_back(v);

    }

}



void solve(){



    read();

    int res = 0;

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

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

    }



    cout<< res << "\n";

}



int main(){



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

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);

    solve();

    return 0;

}


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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

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

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

	return 0;

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

using namespace std;

const int maxn=1e5+5;

int dp[maxn],deg[maxn];

vector<int> G[maxn];



int get(int u)

{

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

	for(auto it:G[u])

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

	return dp[u];

}



int main()

{

	int n,m,x,y;

	cin>>n>>m;

	

	while(m--) 

	{

		 cin>>x>>y;

		 deg[y]++;

		 G[x].push_back(y);

	}

	

	int ans=0;

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

	if(deg[i]==0) ans=max(ans,get(i));

	

	cout<<ans<<endl;

	

	return 0;

 } 

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

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

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

	int u , v;

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

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

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

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

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

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

	cout << ans << endl;

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

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef pair<ll,ll>pll;

typedef pair<ll,pair<ll,ll>>plll;

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

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

#define all(x) x.rbegin(),x.rend()

#define min3(a, b, c)   min(a, min(b, c))

#define max3(a, b, c)   max(a, max(b, c))

#define F first

#define S second

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

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

#define minheap int,vector<int>,greater<int>

#define pb push_back

#define eb emplace_back

#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))

#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))

#define bug cout<<"BUG"<<endl;

const int Max = 1e5 + 10;

const int Mod = 1e9 + 7;

const double PI  =3.141592653589793238463;





ll n,m;



vector<ll>adj[Max+10];



ll indeg[Max+10];



ll dis[Max+10];







void input()

{

      ll i,a,b;





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

       {

           cin>>a>>b;



           adj[a].eb(b);



           indeg[b]++;

       }

}



ll topsort()

{

    ll u,mx=0;



       queue<ll>q;



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

       {

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

       }



         while(!q.empty())

         {

              u=q.front();



              q.pop();



                for(auto v  : adj[u])

                {

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



                   mx=max(dis[v],mx);



                   indeg[v]--;



                   if(!indeg[v])

                    q.push(v);

                }

         }



         return mx;

}

int main()

{



   fastread();



   ll i,j,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r;





     string str;



   bool flag=true;



     cin>>n>>m;



     input();





      cout<<topsort()<<endl;















}