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

using namespace std;



vector<int> adj[100005];

int n,m;



int level[100005];

int dfs(int source){

    if(level[source]!=-1) return level[source];

    int res=0;

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

            int v;

            v=dfs(adj[source][i]);

            res=max(res,v);

    }

    res++;

    level[source]=res;

    return res;

   

}

int main(){

   

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

    cin >> n >> m;

    while(m--){

        int a,b;

        cin >> a >> b;

        adj[a].push_back(b);

    }

    vector<int> xoxo;

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

        xoxo.push_back(dfs(i));

    }

    sort(xoxo.begin(),xoxo.end(),greater<int>());

    

    cout << xoxo[0]-1 << endl;

    

   

    

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

#define f(i,s,v) for(int i=(int)s;i<(int)v;++i)

#define rf(i,s,v) for(int i=(int)s-1;i>(int)v;--i)

#define pb push_back

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

#define memset(c, value) memset(c, (int)value, sizeof(c))

using namespace std;

typedef long long ll;

const ll oo = 1e18;

const ll MOD =1e9+7;

const double pi = acos(-1);

void fast(){

    ios_base :: sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

}

int n, m;

bool valid (int r,int c){

    return (r < n && r >= 0 && c >= 0 && c < m);

}



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

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



vector<int> adj[100001];

int path[100001];

int vis[100001];



int dp(int u){

    if(path[u]!=-1)

        return path[u];



    vis[u] = true;



    int sz = adj[u].size();



    path[u] = 0;

    f(j, 0, sz){

        if(!vis[adj[u][j]])

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

    }

    vis[u] = false;



    return path[u];

}





int main()

{

    fast();



    int u, v;

    cin>>n>>m;

    f(i, 0, m){

        cin>>u>>v;

        adj[u].push_back(v);

    }

    int maxi = 0;

    memset(vis, 0);

    memset(path, -1);



    f(i, 1, n+1){



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

    }



    cout<<maxi<<'\n';

    return 0;

}


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

using namespace std;



vector<int> adj[100005];

int n,m;



int level[100005];

int dfs(int source){

    if(level[source]!=-1) return level[source];

    int res=0;

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

            int v;

            v=dfs(adj[source][i]);

            res=max(res,v);

    }

    res++;

    level[source]=res;

    return res;

   

}

int main(){

   

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

    cin >> n >> m;

    while(m--){

        int a,b;

        cin >> a >> b;

        adj[a].push_back(b);

    }

    vector<int> xoxo;

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

        xoxo.push_back(dfs(i));

    }

    sort(xoxo.begin(),xoxo.end(),greater<int>());

    

    cout << xoxo[0]-1 << endl;

    

   

    

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



typedef   long long  int ll;

typedef  long double ld;

typedef std::vector<ll> vi;

typedef std::vector<std::vector<ll> > vv;



#define MOD 1000000007

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

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

#define mp make_pair

#define pb(x) push_back(x)

#define fi first

#define se second

#define print(vec,a,b) for(ll i=a;i<b;i++) cout<<vec[i]<<" ";cout<<endl;

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

#define input(vec,a,b) for(ll i = a;i<b;i++) cin>>vec[i];

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

using namespace std;



const int N = 1e5 + 5;

vi adj[N];

ll path = 0;

bool vis[N];

stack<ll> arr;

void top_sort(ll u)

{

    vis[u] = true;

    //cout<<"dfs at"<<u<<endl;

    

    for(int to : adj[u])

    {

		

		if(!vis[to])

			top_sort(to);

	}

	//cout<<u<<" has been pushed"<<endl;

	arr.push(u);



	

}

void solve(){

            ll i,j,n,m;

           cin>>n>>m;

           fo(i,0,m)

           {

			   ll a,b;

			   cin>>a>>b;

			   adj[a].pb(b);

		   }

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

		   {

			   if(!vis[i])

			   top_sort(i);

		   }

		   vi ord;

		   while(!arr.empty())

		   {

			   ord.pb(arr.top());

			   arr.pop();

		   }

		   //print(ord,0,ord.size());

		   ms(vis,0);

		   ll dis[n+1] ={0}; 

		   for(auto it : ord)

		   {

			   for(auto to : adj[it])

			   {

				   //cout<<it<<" "<<to<<endl;

				   dis[to] = max(dis[to],dis[it]+ 1);

			   }

			     

			}

			//print(dis,1,n+1);

			cout<<*max_element(dis+1,dis+n+1)<<endl;

		   

		   

            

						            



















}



int main()

{

    IO;

      ll t=1;

      //cin>>t;





      while(t--)

      {



          solve();

      }

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



typedef   long long  int ll;

typedef  long double ld;

typedef std::vector<ll> vi;

typedef std::vector<std::vector<ll> > vv;



#define MOD 1000000007

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

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

#define mp make_pair

#define pb(x) push_back(x)

#define fi first

#define se second

#define print(vec,a,b) for(ll i=a;i<b;i++) cout<<vec[i]<<" ";cout<<endl;

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

#define input(vec,a,b) for(ll i = a;i<b;i++) cin>>vec[i];

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

using namespace std;



const int N = 1e5 + 5;

vi adj[N];

ll path = 0;

bool vis[N];

stack<ll> arr;

void top_sort(ll u)

{

    vis[u] = true;

    //cout<<"dfs at"<<u<<endl;

    

    for(int to : adj[u])

    {

		

		if(!vis[to])

			top_sort(to);

	}

	//cout<<u<<" has been pushed"<<endl;

	arr.push(u);



	

}

void solve(){

            ll i,j,n,m;

           cin>>n>>m;

           fo(i,0,m)

           {

			   ll a,b;

			   cin>>a>>b;

			   adj[a].pb(b);

		   }

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

		   {

			   if(!vis[i])

			   top_sort(i);

		   }

		   vi ord;

		   while(!arr.empty())

		   {

			   ord.pb(arr.top());

			   arr.pop();

		   }

		   //print(ord,0,ord.size());

		   ms(vis,0);

		   ll dis[n+1] ={0}; 

		   for(auto it : ord)

		   {

			   for(auto to : adj[it])

			   {

				   //cout<<it<<" "<<to<<endl;

				   dis[to] = max(dis[to],dis[it]+ 1);

			   }

			     

			}

			//print(dis,1,n+1);

			cout<<*max_element(dis+1,dis+n+1)<<endl;

		   

		   

            

						            



















}



int main()

{

    IO;

      ll t=1;

      //cin>>t;





      while(t--)

      {



          solve();

      }

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



typedef   long long  int ll;

typedef  long double ld;

typedef std::vector<ll> vi;

typedef std::vector<std::vector<ll> > vv;



#define MOD 1000000007

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

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

#define mp make_pair

#define pb(x) push_back(x)

#define fi first

#define se second

#define print(vec,a,b) for(ll i=a;i<b;i++) cout<<vec[i]<<" ";cout<<endl;

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

#define input(vec,a,b) for(ll i = a;i<b;i++) cin>>vec[i];

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

using namespace std;



const int N = 1e5 + 5;

vi adj[N];

ll path = 0;

bool vis[N];

stack<ll> arr;

void top_sort(ll u)

{

    vis[u] = true;

    //cout<<"dfs at"<<u<<endl;

    

    for(int to : adj[u])

    {

		

		if(!vis[to])

			top_sort(to);

	}

	//cout<<u<<" has been pushed"<<endl;

	arr.push(u);



	

}

void solve(){

            ll i,j,n,m;

           cin>>n>>m;

           fo(i,0,m)

           {

			   ll a,b;

			   cin>>a>>b;

			   adj[a].pb(b);

		   }

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

		   {

			   if(!vis[i])

			   top_sort(i);

		   }

		   vi ord;

		   while(!arr.empty())

		   {

			   ord.pb(arr.top());

			   arr.pop();

		   }

		   //print(ord,0,ord.size());

		   ms(vis,0);

		   ll dis[n+1] ={0}; 

		   for(auto it : ord)

		   {

			   for(auto to : adj[it])

			   {

				   //cout<<it<<" "<<to<<endl;

				   dis[to] = max(dis[to],dis[it]+ 1);

			   }

			     

			}

			//print(dis,1,n+1);

			cout<<*max_element(dis+1,dis+n+1)<<endl;

		   

		   

            

						            



















}



int main()

{

    IO;

      ll t=1;

      //cin>>t;





      while(t--)

      {



          solve();

      }

    return 0;

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

#define f(i,s,v) for(int i=(int)s;i<(int)v;++i)

#define rf(i,s,v) for(int i=(int)s-1;i>(int)v;--i)

#define pb push_back

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

#define memset(c, value) memset(c, (int)value, sizeof(c))

using namespace std;

typedef long long ll;

const ll oo = 1e18;

const ll MOD =1e9+7;

const double pi = acos(-1);

void fast(){

    ios_base :: sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

}

int n, m;

bool valid (int r,int c){

    return (r < n && r >= 0 && c >= 0 && c < m);

}



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

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



vector<int> adj[100001];

int path[100001];

int vis[100001];



int dp(int u){

    if(path[u]!=-1)

        return path[u];



    vis[u] = true;



    int sz = adj[u].size();



    path[u] = 0;

    f(j, 0, sz){

        if(!vis[adj[u][j]])

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

    }

    vis[u] = false;



    return path[u];

}





int main()

{

    fast();



    int u, v;

    cin>>n>>m;

    f(i, 0, m){

        cin>>u>>v;

        adj[u].push_back(v);

    }

    int maxi = 0;

    memset(vis, 0);

    memset(path, -1);



    f(i, 1, n+1){



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

    }



    cout<<maxi<<'\n';

    return 0;

}


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



typedef   long long  int ll;

typedef  long double ld;

typedef std::vector<ll> vi;

typedef std::vector<std::vector<ll> > vv;



#define MOD 1000000007

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

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

#define mp make_pair

#define pb(x) push_back(x)

#define fi first

#define se second

#define print(vec,a,b) for(ll i=a;i<b;i++) cout<<vec[i]<<" ";cout<<endl;

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

#define input(vec,a,b) for(ll i = a;i<b;i++) cin>>vec[i];

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

using namespace std;



const int N = 1e5 + 5;

vi adj[N];

ll path = 0;

bool vis[N];

stack<ll> arr;

void top_sort(ll u)

{

    vis[u] = true;

    //cout<<"dfs at"<<u<<endl;

    

    for(int to : adj[u])

    {

		

		if(!vis[to])

			top_sort(to);

	}

	//cout<<u<<" has been pushed"<<endl;

	arr.push(u);



	

}

void solve(){

            ll i,j,n,m;

           cin>>n>>m;

           fo(i,0,m)

           {

			   ll a,b;

			   cin>>a>>b;

			   adj[a].pb(b);

		   }

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

		   {

			   if(!vis[i])

			   top_sort(i);

		   }

		   vi ord;

		   while(!arr.empty())

		   {

			   ord.pb(arr.top());

			   arr.pop();

		   }

		   //print(ord,0,ord.size());

		   ms(vis,0);

		   ll dis[n+1] ={0}; 

		   for(auto it : ord)

		   {

			   for(auto to : adj[it])

			   {

				   //cout<<it<<" "<<to<<endl;

				   dis[to] = max(dis[to],dis[it]+ 1);

			   }

			     

			}

			//print(dis,1,n+1);

			cout<<*max_element(dis+1,dis+n+1)<<endl;

		   

		   

            

						            



















}



int main()

{

    IO;

      ll t=1;

      //cin>>t;





      while(t--)

      {



          solve();

      }

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

using namespace std;

int N, M, x, y;

vector <int> adj[100005];

int dis[100005];

using namespace std;

int dfs(int cur){

	if(dis[cur] != 1) return dis[cur];

	int maxtemp = 0;

	for(auto j : adj[cur]){

		maxtemp = max(maxtemp, dfs(j));

	}

	dis[cur] += maxtemp;

	return dis[cur];

}

int main(){

	scanf("%d%d", &N, &M);

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

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

		adj[x].push_back(y);

	}

	fill(dis,dis+N+1,1);

	int maxdis = 0;

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

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

	}

	cout << maxdis -1 <<endl;

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

using namespace std;

int N, M, x, y;

vector <int> adj[100005];

int dis[100005];

using namespace std;

int dfs(int cur){

	if(dis[cur] != 1) return dis[cur];

	int maxtemp = 0;

	for(auto j : adj[cur]){

		maxtemp = max(maxtemp, dfs(j));

	}

	dis[cur] += maxtemp;

	return dis[cur];

}

int main(){

	scanf("%d%d", &N, &M);

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

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

		adj[x].push_back(y);

	}

	fill(dis,dis+N+1,1);

	int maxdis = 0;

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

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

	}

	cout << maxdis -1 <<endl;

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

using namespace std;

int N, M, x, y;

vector <int> adj[100005];

int dis[100005];

using namespace std;

int dfs(int cur){

	if(dis[cur] != 1) return dis[cur];

	int maxtemp = 0;

	for(auto j : adj[cur]){

		maxtemp = max(maxtemp, dfs(j));

	}

	dis[cur] += maxtemp;

	return dis[cur];

}

int main(){

	scanf("%d%d", &N, &M);

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

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

		adj[x].push_back(y);

	}

	fill(dis,dis+N+1,1);

	int maxdis = 0;

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

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

	}

	cout << maxdis -1 <<endl;

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

using namespace std;

int N, M, x, y;

vector <int> adj[100005];

int dis[100005];

using namespace std;

int dfs(int cur){

	if(dis[cur] != 1) return dis[cur];

	int maxtemp = 0;

	for(auto j : adj[cur]){

		maxtemp = max(maxtemp, dfs(j));

	}

	dis[cur] += maxtemp;

	return dis[cur];

}

int main(){

	scanf("%d%d", &N, &M);

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

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

		adj[x].push_back(y);

	}

	fill(dis,dis+N+1,1);

	int maxdis = 0;

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

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

	}

	cout << maxdis -1 <<endl;

}