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

using namespace std;



vector<int>v[100001];

int dp[100001];



int fun(int src){

       

       if(dp[src] != -1)

          return dp[src];



          bool leaf = true;

          int bestchild = -1;

        for(int nbr : v[src]){

            leaf = false;

            bestchild =  max(bestchild,fun(nbr));

        }

       return dp[src] = leaf? 0 : 1+bestchild;

}



void solve(){

  int n,m;

  cin >> n >> m;

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

      int x , y;

      cin >> x >> y;

      v[x].push_back(y);

  } 

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

       int ans = 0;

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

       { ans = max(ans,fun(i));}

    cout << ans;



}

int main(){

//  #ifndef ONLINE_JUDGE

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

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

// #endif 



solve();

return 0;

}

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

using namespace std;



vector<list<int>>v;





int fun(vector<int> &dp,int src){

       if(v[src].empty())

          {    dp[src] = 0;

               return 0;}



       if(dp[src] != -1)

          return dp[src];



          int bestchild = -1;

        for(int nbr : v[src]){

          

            bestchild =  max(bestchild,fun(dp,nbr));

        }

       return dp[src] = 1+bestchild;

}



void solve(){

  int n,m;

  cin >> n >> m;

  v.resize(n+1);

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

      int x , y;

      cin >> x >> y;

      v[x].push_back(y);

  } 

      vector<int>dp(n+1,-1);

     

       int ans = 0;

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

       { ans = max(ans,fun(dp,i));}



  cout << ans;



}

int main(){

//  #ifndef ONLINE_JUDGE

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

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

// #endif 



solve();

return 0;

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

 

using namespace std;

 

#define lli long long int

#define REP(i,s,l) for(lli i=s;i<l;i++)

#define DEBUG 0

#define INF (1LL<<50)

#define MOD 1000000007



lli dp[100100];

map<lli,vector<lli>> x;



lli dfs(lli p){

	if(dp[p]>0)return dp[p];



	REP(i,0,x[p].size()){

		lli next = x[p][i];

		dp[p] = max(dp[p],dfs(next)+1);

	}

	return dp[p];

}



signed main(){



	lli n,m;

	cin>>n>>m;

	REP(i,0,100100)dp[i]=0;



	REP(i,0,m){

		lli a,b;

		cin>>a>>b;

		a--,b--;

		x[a].push_back(b);

	}



	lli ans=0;

	REP(i,0,n){

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

	}



	cout<<ans<<endl;



	return 0;

}

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



using namespace std;



#define lli long long int

#define REP(i,s,n) for(int i=s;i<n;i++)

#define MOD 1000000007

#define NUM 2520

#define INF (1LL<<50)

#define DEBUG 0

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

#define SORT(V) sort(V.begin(),V.end())

#define PI (3.141592653589794)



lli dp[100100];

bool vis[100100];

map<lli,vector<lli>> d;



lli dfs(lli now){

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

	vis[now]=true;



	lli ret=0;

	for(auto e:d[now]){

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

	}

	return dp[now]=ret;

}



signed main(){



	lli n,m;

	cin>>n>>m;



	REP(i,0,m){

		lli x,y;

		cin>>x>>y;

		x--,y--;

		d[x].push_back(y);

	}



	lli ans=0;

	REP(i,0,n){

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

	}



	cout<<ans<<endl;

	return 0;

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

#include<iomanip>

#include<cmath>

#include<string>

#include<cctype>

#include<algorithm>

#include<set>

#include<map>

#include<stack>

#include<queue>

#include<cstring>

//#include<windows.h>

//#include<time.h>

#define pb push_back

#define ppb pop_back

#define fi first

#define se second

#define mer make_pair

#define mme memset

using namespace std;



const double pie1=3.1415926;//π1

const double pie2=3.14159265358979;//π2

const double pie3=3.14159265358979323846;//π3

const int vbi=999999999;

const int vli=-vbi;

const long long vbl=999999999999999999;

const long long vll=-vbl;



typedef long long ll;

typedef pair<int,int> pii;

typedef pair<ll,ll> pll;

typedef pair<pii,int> piii;

typedef pair<pii,pii> pi4;

typedef pair<pll,ll> plll;

typedef pair<int,pii> ipii;

typedef pair<ll,pll> lpll;

typedef pair<pll,pll> pl4;

typedef vector<int> vi;

typedef vector<ll> vl;

typedef vector<pii> vpi;

typedef vector<pll> vpl;

typedef set<int> sti;

typedef set<ll> stl;

typedef set<pii> spi;

typedef set<pll> spl;

priority_queue<int> pqi;

priority_queue<ll> pql;

queue<int> qi;

queue<ll> ql;



/*void bin_v(){

	char bin[1000];

	gets(bin);

}*/



bool pss(int a){

	for(int i=2;i<=sqrt(a);i++)

		if(a%i==0)

			return false;

	return true;

}

/////////////////////////////////////////////////////////////////////////////////////////

int n,m;

vector<int> v[100010];

int d[100010];

int dp[100010];



int main(){

	//my name is Veex_WCY

	ios::sync_with_stdio(false);

	cin>>n>>m;

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

		int a,b;

		cin>>a>>b;

		v[a].pb(b);

		d[b]++;

	}

	stack<int> st;

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

		if(d[i]==0)

			st.push(i);

	while(!st.empty()){

		int x=st.top();

		st.pop();

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

			int ne=v[x][i];

			if(dp[x]+1>dp[ne])

				dp[ne]=dp[x]+1;

			d[ne]--;

			if(d[ne]==0)

				st.push(ne);

		}

	}

	int maxn=0;

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

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

	cout<<maxn;

	return 0;

}


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

using namespace std;

#define   pb  push_back

queue<int>q;

int main()

{

    int i,j,n,m,x,y,f,mx=0;

    cin>>n>>m;

    int ind[n+2],dp[n+2];

    vector<int>v[n+2];

    memset(ind,0,sizeof(ind));

    memset(dp,0,sizeof(dp));

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

    {

        cin>>x>>y;

        v[x].pb(y);

        ind[y]++;

    }

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

    {

        if(ind[i]==0)

        {

            q.push(i);

        }

    }

    while(!q.empty())

    {

        f=q.front();

        q.pop();

        x=v[f].size();

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

        {

            y=v[f][i];

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

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

            ind[y]--;

            if(ind[y]==0)

            {

                q.push(y);

            }

        }

    }

    cout<<mx<<"\n";

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



using namespace std;



#define ll long long int



int memo[(int)1e5 + 1];

vector<int> adj[(int)1e5 + 1];



bool vis[(int)1e5 + 1];



int dfs(int x) {

	

	if(vis[x])return memo[x];



	vis[x] = true;

	

	int temp = 1;



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

		memo[x] = max(memo[x], temp + dfs(adj[x][i]));

	}



	return memo[x];

}



int main() {



	ios_base::sync_with_stdio(0);

	cin.tie(0);

	

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

	int s,t;

	

	while(m--) {

		cin >> s >> t;

		adj[s].push_back(t);

	}



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

		dfs(i);

	}



	int ans = 0;

	

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

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

	}



	cout << ans << '\n';



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

using namespace std;



const int N = 1e5 + 100;





#define mp make_pair

#define f first

#define s second



#define pb push_back



void boost()

{

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);

}



int n,m;



vector<int>g[N];



bool was[N];



int d[N];



vector<int>ord;

int ans;



void dfs(int v)

{

 	was[v] = 1;

 	for (int to :g[v])

 	{

 		if (!was[to])

 		{

 			dfs(to);

 		}

 	}

 	ord.pb(v);

}

                                                                                                          

void dfs1(int v)

{

 	was[v] = 1;

 	for (int to :g[v])

 	{

 		if (!was[to])

 		{

 			dfs1(to);

 		}                               

 		d[v] = max(d[v],d[to] + 1);

 		ans = max(ans,d[v]);

 	}                          

}





int main(){

	boost();

	cin >> n >> m;

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

	{

		int u,v;

		cin >> u >> v;

		g[u].pb(v);

	}

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

	{

		if (!was[i])

		{             

			dfs(i);

		}

	}

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

	{

		was[i] = 0;

	}

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

	{                        

		dfs1(ord[i]);            

	}

	cout << ans;       

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



using namespace std;



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

#define DB( x ) cout << #x << " = " << x << endl;

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

#define endl '\n'



typedef long long ll;

typedef unsigned long long ull;

typedef pair<ll, ll> pii;

typedef pair<int, pii> tri;

typedef vector <vector <ll>> matrix;

typedef pair<string, string> pss;



const ll mod = (ll)1e9 + 7;

const int MAX = (int)3e3 + 100;



const int oo = 0x3f3f3f3f;



int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);



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



    vector<vector<int> > g(n + 1);



    vector<int> in(n + 1, 0);



    vector<ll> dp(n + 1, 0);



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

        int a, b; cin >> a >> b;

        g[a].push_back(b);

        in[b]++;

    }



    queue<int> Q;



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

        if(!in[i])

            Q.push(i);



    vector<int> order;



    while(!Q.empty()){

        int curr = Q.front(); Q.pop();

        order.push_back(curr);

        for(auto newn : g[curr]){

            in[newn]--;

            if(!in[newn])

                Q.push(newn);

        }

    }



    ll solve = 0;



    for(auto node : order)

        for(auto newn : g[node])

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

    for(auto node : order)

        solve = max(solve, dp[node]);



    cout << solve << endl;

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



using namespace std;



#define ll long long int



int memo[(int)1e5 + 1];

vector<int> adj[(int)1e5 + 1];



bool vis[(int)1e5 + 1];



int dfs(int x) {

	

	if(vis[x])return memo[x];



	vis[x] = true;

	

	int temp = 1;



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

		memo[x] = max(memo[x], temp + dfs(adj[x][i]));

	}



	return memo[x];

}



int main() {



	ios_base::sync_with_stdio(0);

	cin.tie(0);

	

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

	int s,t;

	

	while(m--) {

		cin >> s >> t;

		adj[s].push_back(t);

	}



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

		dfs(i);

	}



	int ans = 0;

	

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

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

	}



	cout << ans << '\n';



	return 0;

}

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



using namespace std;



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

#define DB( x ) cout << #x << " = " << x << endl;

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

#define endl '\n'



typedef long long ll;

typedef unsigned long long ull;

typedef pair<ll, ll> pii;

typedef pair<int, pii> tri;

typedef vector <vector <ll>> matrix;

typedef pair<string, string> pss;



const ll mod = (ll)1e9 + 7;

const int MAX = (int)3e3 + 100;



const int oo = 0x3f3f3f3f;



int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);



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



    vector<vector<int> > g(n + 1);



    vector<int> in(n + 1, 0);



    vector<ll> dp(n + 1, 0);



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

        int a, b; cin >> a >> b;

        g[a].push_back(b);

        in[b]++;

    }



    queue<int> Q;



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

        if(!in[i])

            Q.push(i);



    vector<int> order;



    while(!Q.empty()){

        int curr = Q.front(); Q.pop();

        order.push_back(curr);

        for(auto newn : g[curr]){

            in[newn]--;

            if(!in[newn])

                Q.push(newn);

        }

    }



    ll solve = 0;



    for(auto node : order)

        for(auto newn : g[node])

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

    for(auto node : order)

        solve = max(solve, dp[node]);



    cout << solve << endl;

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

using namespace std;



const int N = 1e5 + 100;





#define mp make_pair

#define f first

#define s second



#define pb push_back



void boost()

{

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);

}



int n,m;



vector<int>g[N];



bool was[N];



int d[N];



vector<int>ord;

int ans;



void dfs(int v)

{

 	was[v] = 1;

 	for (int to :g[v])

 	{

 		if (!was[to])

 		{

 			dfs(to);

 		}

 	}

 	ord.pb(v);

}

                                                                                                          

void dfs1(int v)

{

 	was[v] = 1;

 	for (int to :g[v])

 	{

 		if (!was[to])

 		{

 			dfs1(to);

 		}                               

 		d[v] = max(d[v],d[to] + 1);

 		ans = max(ans,d[v]);

 	}                          

}





int main(){

	boost();

	cin >> n >> m;

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

	{

		int u,v;

		cin >> u >> v;

		g[u].pb(v);

	}

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

	{

		if (!was[i])

		{             

			dfs(i);

		}

	}

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

	{

		was[i] = 0;

	}

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

	{                        

		dfs1(ord[i]);            

	}

	cout << ans;       

}

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



using namespace std;



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

#define DB( x ) cout << #x << " = " << x << endl;

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

#define endl '\n'



typedef long long ll;

typedef unsigned long long ull;

typedef pair<ll, ll> pii;

typedef pair<int, pii> tri;

typedef vector <vector <ll>> matrix;

typedef pair<string, string> pss;



const ll mod = (ll)1e9 + 7;

const int MAX = (int)3e3 + 100;



const int oo = 0x3f3f3f3f;



int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);



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



    vector<vector<int> > g(n + 1);



    vector<int> in(n + 1, 0);



    vector<ll> dp(n + 1, 0);



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

        int a, b; cin >> a >> b;

        g[a].push_back(b);

        in[b]++;

    }



    queue<int> Q;



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

        if(!in[i])

            Q.push(i);



    vector<int> order;



    while(!Q.empty()){

        int curr = Q.front(); Q.pop();

        order.push_back(curr);

        for(auto newn : g[curr]){

            in[newn]--;

            if(!in[newn])

                Q.push(newn);

        }

    }



    ll solve = 0;



    for(auto node : order)

        for(auto newn : g[node])

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

    for(auto node : order)

        solve = max(solve, dp[node]);



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

/**

*   author: j o s e m a g o

*   date:

**/

using namespace std;

#pragma GCC optimize ("Ofast")

#define f first

#define s second

#define mp make_pair

#define lb lower_bound

#define ub upper_bound

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

#define mset(a,v) memset(a,v,sizeof(a))

#define pb push_back

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

#define rsz resize

#define ln "\n"



typedef double db;

typedef unsigned long long ull;

typedef long long ll;

typedef long double ld;

const ld PI = 2*acos(0.0);

const int MOD = 1000000007;

const int JJJ = 100009;



int N,A;

vector<int>grafo[JJJ];

bool visited[JJJ];

int dp[JJJ];



int sol = 0;

void dfs(int nod){

    visited[nod] = true;

    dp[nod] = -1;

    for(int i = 0;i < (int)grafo[nod].size();i++){

        int xx = grafo[nod][i];

        if(!visited[xx]){

            dfs(xx);

        }

        dp[nod] = max(dp[nod],dp[xx]);

    }

    dp[nod]++;

}







int main(){



  

    ios_base::sync_with_stdio(0); cin.tie(0);





    cin >> N >> A;

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

        int a,b;

        cin >> a >> b;

        grafo[a].pb(b);

    }



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

        if(!visited[i]){

            dfs(i);

        }

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

    }



    cout<<sol;







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

using namespace std;

const int N = 1e5 + 25, M = 1e6 + 25, oo = 1e9, mod = 1e9 + 7;

const long long OO = 1e18+25;



int n, m,u,v, mx, mem[N];

vector <int> g[N];



int dp(int p)

{

    int &ret = mem[p];

    if(ret != 0) return ret;

    for(int to : g[p])

        ret = max(ret, dp(to)+1);

    return ret;

}



int main()

{

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

    while(m--)

    {

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

        g[u].push_back(v);

    }

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

    {

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

    }

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

    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: 3.154594418382448x):
#include <iostream>

#include <algorithm>

#include <cmath>

#include <cstdlib>

#include <cstdio>

#include <cstring>

#include <iomanip>

#include <string>

#include <set>

#include <vector>

#include <queue>

#include <stack>

#include <map>

using namespace std;

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

#define fore(i, a, n) for (long long i = a; i < n; i++)

#define ford(i, a, n) for (long long i = n - 1; i >= a; i--)

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

#define sl(a) scanf("%lld", &a)

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

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

#define sll(a, b) scanf("%lld%lld", &a, &b)

#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)

#define debug(a) cout << a << endl

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

#define endl '\n'

#define pi acos(-1.0)

#define tr t[root]

#define lson t[root << 1]

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

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

#define ull unsigned long long

const double eps = 1e-8;

inline int sgn(const double &x) { return x < -eps ? -1 : x > eps; }

typedef long long ll;

const int inf=0x3f3f3f3f;

const int MAXN = 1e5 + 10;

int n,m;

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

ll dfs(int x)

{

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

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

	ll ans=0;

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

	{

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

				

	}//cout<<ans<<endl;

	

	return dp[x]=ans;

}

int main()

{

	sii(n,m);

	ms(head,-1);

	ll ans=0;

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

	{

		int x,y;

		sii(x,y);

		add(x,y);

		ru[y]++;

		chu[x]++;

	}

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

	{

		if(!ru[i])

		{

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

		}

	}

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

 }