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

using namespace std;



const int maxn = 1e5 + 5;



bool visited[maxn];

int dist[maxn];

vector<int> g[maxn];

int indegree[maxn];



void dfs(int a){

    visited[a] = true;

    for(auto b : g[a]){

        dist[b] = max(dist[b] , 1 + dist[a]);

        --indegree[b];

        if(indegree[b] == 0)

            dfs(b);

    }

}



int main(){

    int n,m;

    cin>>n>>m;



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

        int a,b;

        cin>>a>>b;



        g[a].push_back(b);

        indegree[b]++;

    }



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

        if(!visited[i] && indegree[i] == 0)

            dfs(i);

    }



    cout<<*max_element(dist,dist+maxn);

}

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



#define int long long

using namespace std;

#define maxn 100010

int n,m;

vector<int> g[maxn];

int indegree[maxn] ,dist[maxn] ;

bool visited[maxn];



void dfs(int v,int p){

    visited[v] = true;

    for(auto x: g[v]){

        if(x == p)continue;

        dist[x] = max(dist[x] , 1 + dist[v]);

        indegree[x]--;

        if(indegree[x] == 0 and !visited[x])

            dfs(x ,v);

    }

}



int32_t main() {



    cin >> n >> m;



    int a ,b;

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

        cin >> a >> b;

        g[a].push_back(b);

        indegree[b]++;

    }

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

        if(!visited[i] and indegree[i] == 0)

            dfs(i , -1);

    }

    int ans = 0;

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

        ans = max(ans,dist[j]);

    }

    cout << ans;

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

#include <vector>

using namespace std;

#define NMAX 100000



// 入力

int N, M;

int dp[NMAX];        // メモ化再帰

vector<int> G[NMAX]; // グラフ

// vector<vector<int>> G; // グラフ



inline int rec(int v) {

    if (dp[v] != 0)

        return dp[v]; // 既に更新済み



    int res = 0;

    for (auto nv : G[v]) {

        res = max(res, rec(nv) + 1);

    }

    return dp[v] = res; // メモしながらリターン

}



int main() {

    // 入力受け取り

    cin >> N >> M;

    // G.assign(N, vector<int>());

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

        int x, y;

        cin >> x >> y;

        --x, --y; // 0-indexed にする

        G[x].push_back(y);

    }



    // 全ノードを一通り更新しながら答えを求める

    int res = 0;

    for (int v = 0; v < N; ++v) {

        res = max(res, rec(v));

    }

    cout << res << endl;

}

// Faster C++ solution for the same problem (measured speedup: 3.4850410323823855x):
#include <algorithm>

#include <iostream>

#include <map>

#include <set>

#include <vector>

using namespace std;

typedef long long ll;

typedef pair<int, int> PII;

typedef pair<ll, ll> PLL;



vector<int> dp; // 頂点 i から始まるパスで最長のものの長さ

multimap<int, int> mp; // 入力値(from, to)



// 一番遠い場所を探す

inline int rec(int from) {

    if (dp[from] != 0) {

        return dp[from];

    }

    int res = 0;

    // キーを持つ値を列挙する

    auto p = mp.equal_range(from);

    for (auto it = p.first; it != p.second; ++it) {

        res = max(res, rec(it->second) + 1);

    }

    return dp[from] = res;

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(0);

    int n, m, ans = 0;

    cin >> n >> m;

    dp.resize(n + 1, 0);

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

        int x, y;

        cin >> x >> y;

        mp.emplace(x, y);

    }

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

        if (dp[i] == 0) {

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

        }

    }

    cout << ans << endl;

    return 0;

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

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;



const int MAXN=100005;

vector <int> E[MAXN];

int In[MAXN];

int Q[MAXN], head, tail;

int n, m, dp[MAXN];



int main()

{

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

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

		int a, b;

		scanf ("%d%d", &a, &b);

		E[a].push_back (b);

		In[b]++;

	}

	

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

		if (!In[i])

			Q[++tail] = i;

	

	int res=0;

	while (head < tail){

		int x=Q[++head];

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

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

			int y=E[x][i];

			In[y]--;

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

			if (!In[y])  Q[++tail] = y;

		}

	}

	

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

	return 0;

}

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

typedef long long ll;

using namespace std;

const int N = 1e5+10 , M = 5e5+10, OO = 0x3f3f3f3f ,  MOD = 1e9+7;

const double EPS = 1e-3;

#define M_PI 3.14159265358979323846

#define clr(arr,val) memset(arr , val , sizeof (arr))

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

#define rloop(i,n) for(int i = int(n)-1;i >= 0;i--)

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

#define pb push_back

#define F first

#define S second

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

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

typedef pair <int,int> pii ;

typedef vector < pii > vii ;

typedef vector < int > vi;

int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction

//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction

//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction

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

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





int n , m ,u, v, nxt[N],to[N],head[N],ne;

int dp[N];



void init(){



    ne = 0;

    memset(head, -1 , sizeof head);

}



void addEdge(int f, int t){



    to[ne] = t;

    nxt[ne] = head[f];

    head[f] = ne++;

}



int solve(int idx){



   // if(idx == n) return 0;



    int& ret = dp[idx];

    if(~ret) return ret;

    //if(~ret) return 0;



    ret = 0;

    for (int e = head[idx]; ~e ; e = nxt[e])

        ret = max(ret, solve(to[e])+1);





    return ret;



}







int main(){





    //ios_base::sync_with_stdio(0);

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





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

    init();



    loop(i , m){

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

        addEdge(u, v);

    }



    int mx = 0;

    memset(dp, -1 , sizeof dp);





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

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





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



    return 0;

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

using namespace std;

#define print printf("==================\n")

#define ll long long

#define pi acos(-1.0)

#define eps 1e-16

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

const ll INF = 1 << 30;

typedef pair<double, double  > payar;

typedef struct

{

    ll  x, y;

} point;

priority_queue<payar, vector<payar>,  greater<payar> > pq; ///accending

vector< pair<double,payar  > > vpp;

vector < payar >  vp;

vector<int>g[100005];

int ind[100005];

int vis[100005];

int dis[100005];



int dfs(int u)

{



    vis[u]=1;

    if(dis[u]!=-1)return dis[u];

    int maxi =0;

    for(auto it: g[u])

    {

       // static int k;

     //   cout<<k++<<" "<<it<<endl;



      dis[u]=  maxi=max(maxi,1+dfs(it) );

    }

    return maxi;

}

int main()

{



    int n, m ;

    cin>>n>>m;

    memset(dis,-1,sizeof dis);

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

    {

        int u, v;

        cin>>u>>v;

        g[u].push_back(v);

        ind[v]++;



    }

    int ans =0;

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

    {

        if(ind[i]==0 and vis[i]==0)

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

    }

    cout<<ans;

    return  0 ;

}


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

typedef long long ll;

using namespace std;

const int N = 1e5+10 , M = 5e5+10, OO = 0x3f3f3f3f ,  MOD = 1e9+7;

const double EPS = 1e-3;

#define M_PI 3.14159265358979323846

#define clr(arr,val) memset(arr , val , sizeof (arr))

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

#define rloop(i,n) for(int i = int(n)-1;i >= 0;i--)

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

#define pb push_back

#define F first

#define S second

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

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

typedef pair <int,int> pii ;

typedef vector < pii > vii ;

typedef vector < int > vi;

int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction

//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction

//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction

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

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





int n , m ,u, v, nxt[N],to[N],head[N],ne;

int dp[N];



void init(){



    ne = 0;

    memset(head, -1 , sizeof head);

}



void addEdge(int f, int t){



    to[ne] = t;

    nxt[ne] = head[f];

    head[f] = ne++;

}



int solve(int idx){



   // if(idx == n) return 0;



    int& ret = dp[idx];

    if(~ret) return ret;

    //if(~ret) return 0;



    ret = 0;

    for (int e = head[idx]; ~e ; e = nxt[e])

        ret = max(ret, solve(to[e])+1);





    return ret;



}







int main(){





    //ios_base::sync_with_stdio(0);

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





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

    init();



    loop(i , m){

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

        addEdge(u, v);

    }



    int mx = 0;

    memset(dp, -1 , sizeof dp);





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

        mx = max(mx , solve(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;

}

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

typedef long long ll;

using namespace std;

const int N = 1e5+10 , M = 5e5+10, OO = 0x3f3f3f3f ,  MOD = 1e9+7;

const double EPS = 1e-3;

#define M_PI 3.14159265358979323846

#define clr(arr,val) memset(arr , val , sizeof (arr))

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

#define rloop(i,n) for(int i = int(n)-1;i >= 0;i--)

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

#define pb push_back

#define F first

#define S second

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

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

typedef pair <int,int> pii ;

typedef vector < pii > vii ;

typedef vector < int > vi;

int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction

//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction

//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction

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

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





int n , m ,u, v, nxt[N],to[N],head[N],ne;

int dp[N];



void init(){



    ne = 0;

    memset(head, -1 , sizeof head);

}



void addEdge(int f, int t){



    to[ne] = t;

    nxt[ne] = head[f];

    head[f] = ne++;

}



int solve(int idx){



   // if(idx == n) return 0;



    int& ret = dp[idx];

    if(~ret) return ret;

    //if(~ret) return 0;



    ret = 0;

    for (int e = head[idx]; ~e ; e = nxt[e])

        ret = max(ret, solve(to[e])+1);





    return ret;



}







int main(){





    //ios_base::sync_with_stdio(0);

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





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

    init();



    loop(i , m){

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

        addEdge(u, v);

    }



    int mx = 0;

    memset(dp, -1 , sizeof dp);





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

        mx = max(mx , solve(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;

}//z

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

typedef long long ll;

using namespace std;

const int N = 1e5+10 , M = 5e5+10, OO = 0x3f3f3f3f ,  MOD = 1e9+7;

const double EPS = 1e-3;

#define M_PI 3.14159265358979323846

#define clr(arr,val) memset(arr , val , sizeof (arr))

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

#define rloop(i,n) for(int i = int(n)-1;i >= 0;i--)

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

#define pb push_back

#define F first

#define S second

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

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

typedef pair <int,int> pii ;

typedef vector < pii > vii ;

typedef vector < int > vi;

int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction

//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction

//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

//int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal Direction

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

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





int n , m ,u, v, nxt[N],to[N],head[N],ne;

int dp[N];



void init(){



    ne = 0;

    memset(head, -1 , sizeof head);

}



void addEdge(int f, int t){



    to[ne] = t;

    nxt[ne] = head[f];

    head[f] = ne++;

}



int solve(int idx){



   // if(idx == n) return 0;



    int& ret = dp[idx];

    if(~ret) return ret;

    //if(~ret) return 0;



    ret = 0;

    for (int e = head[idx]; ~e ; e = nxt[e])

        ret = max(ret, solve(to[e])+1);





    return ret;



}







int main(){





    //ios_base::sync_with_stdio(0);

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





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

    init();



    loop(i , m){

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

        addEdge(u, v);

    }



    int mx = 0;

    memset(dp, -1 , sizeof dp);





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

        mx = max(mx , solve(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;

}

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





using namespace std;



vector<int> v[100005];

bool processado[100005];

int tab[100005];



int dfs(int s)

{



    //processado[s] = true;

    int soma, max = 0, entrou = 0;

    

    if(tab[s] >= 0)

    {

        return tab[s];

    }

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

    {



        entrou = 1;

        soma = dfs(v[s][i]) + 1;

        if(soma > max)

        {

            max = soma;

            tab[s] = max;

        }

    }

    if(entrou == 1)

    {

        return max;

    }



    return tab[s] = 0;



}

 





int main(){



    int n, m;

    cin >> n >> m;

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



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

    {

        processado[i] = false;

    }*/



    int x, y;

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

    {

        cin >> x >> y;

        v[x].push_back(y);

    }



    int soma = 0;

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

    {

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

    }



    cout << soma << 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;

}//z

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





using namespace std;



vector<int> v[100005];

bool processado[100005];

int tab[100005];



int dfs(int s)

{



    //processado[s] = true;

    int soma, max = 0, entrou = 0;

    

    if(tab[s] >= 0)

    {

        return tab[s];

    }

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

    {



        entrou = 1;

        soma = dfs(v[s][i]) + 1;

        if(soma > max)

        {

            max = soma;

            tab[s] = max;

        }

    }

    if(entrou == 1)

    {

        return max;

    }



    return tab[s] = 0;



}

 





int main(){



    int n, m;

    cin >> n >> m;

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



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

    {

        processado[i] = false;

    }*/



    int x, y;

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

    {

        cin >> x >> y;

        v[x].push_back(y);

    }



    int soma = 0;

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

    {

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

    }



    cout << soma << endl;











    



    

    return 0;

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

using namespace std;

#define print printf("==================\n")

#define ll long long

#define pi acos(-1.0)

#define eps 1e-16

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

const ll INF = 1 << 30;

typedef pair<double, double  > payar;

typedef struct

{

    ll  x, y;

} point;

priority_queue<payar, vector<payar>,  greater<payar> > pq; ///accending

vector< pair<double,payar  > > vpp;

vector < payar >  vp;

vector<int>g[100005];

int ind[100005];

int vis[100005];

int dis[100005];



int dfs(int u)

{



    vis[u]=1;

    if(dis[u]!=-1)return dis[u];

    int maxi =0;

    for(auto it: g[u])

    {

       // static int k;

     //   cout<<k++<<" "<<it<<endl;



      dis[u]=  maxi=max(maxi,1+dfs(it) );

    }

    return maxi;

}

int main()

{



    int n, m ;

    cin>>n>>m;

    memset(dis,-1,sizeof dis);

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

    {

        int u, v;

        cin>>u>>v;

        g[u].push_back(v);

        ind[v]++;



    }

    int ans =0;

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

    {

        if(ind[i]==0 and vis[i]==0)

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

    }

    cout<<ans;

    return  0 ;

}


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



using namespace std;



vector < int > adj[100007];



int used[100007], dp[100007], mx;



void dfs( int v ) {

	if ( used[v] ) return;

	used[v] = 1;

	for ( auto u: adj[v] ) {

		dfs(u);

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

	}

}



int main() {

	int n, m;

	cin >> n >> m;

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

		int u, v;

		cin >> u >> v;

		adj[u].push_back(v);

	}

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

		dfs(i);

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

	}

	cout << mx << 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;

}

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



using namespace std;



vector < int > adj[100007];



int used[100007], dp[100007], mx;



void dfs( int v ) {

	if ( used[v] ) return;

	used[v] = 1;

	for ( auto u: adj[v] ) {

		dfs(u);

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

	}

}



int main() {

	int n, m;

	cin >> n >> m;

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

		int u, v;

		cin >> u >> v;

		adj[u].push_back(v);

	}

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

		dfs(i);

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

	}

	cout << mx << 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;

}//z

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



using namespace std;



vector < int > adj[100007];



int used[100007], dp[100007], mx;



void dfs( int v ) {

	if ( used[v] ) return;

	used[v] = 1;

	for ( auto u: adj[v] ) {

		dfs(u);

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

	}

}



int main() {

	int n, m;

	cin >> n >> m;

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

		int u, v;

		cin >> u >> v;

		adj[u].push_back(v);

	}

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

		dfs(i);

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

	}

	cout << mx << endl;

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





using namespace std;



vector<int> v[100005];

bool processado[100005];

int tab[100005];



int dfs(int s)

{



    //processado[s] = true;

    int soma, max = 0, entrou = 0;

    

    if(tab[s] >= 0)

    {

        return tab[s];

    }

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

    {



        entrou = 1;

        soma = dfs(v[s][i]) + 1;

        if(soma > max)

        {

            max = soma;

            tab[s] = max;

        }

    }

    if(entrou == 1)

    {

        return max;

    }



    return tab[s] = 0;



}

 





int main(){



    int n, m;

    cin >> n >> m;

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



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

    {

        processado[i] = false;

    }*/



    int x, y;

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

    {

        cin >> x >> y;

        v[x].push_back(y);

    }



    int soma = 0;

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

    {

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

    }



    cout << soma << endl;











    



    

    return 0;

}


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



using namespace std;



vector < int > adj[100007];



int used[100007], dp[100007], mx;



void dfs( int v ) {

	if ( used[v] ) return;

	used[v] = 1;

	for ( auto u: adj[v] ) {

		dfs(u);

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

	}

}



int main() {

	int n, m;

	cin >> n >> m;

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

		int u, v;

		cin >> u >> v;

		adj[u].push_back(v);

	}

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

		dfs(i);

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

	}

	cout << mx << endl;

}