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

using namespace std;

#define N (int)2e5

#define fi first

#define se second

#define mp make_pair

#define pb push_back

int n,m,memo[N];

vector <int> adj[N];



int dp(int u) {

    if (adj[u].size() == 0) return 0;

    int &sol = memo[u];

    if (sol != -1) return sol;

    int keep = INT_MIN;

    for (int v : adj[u]) {

        keep = max(

            keep ,

            dp(v) + 1

        );

    }

    return sol = keep;

}



int main() {

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

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

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

        adj[u].pb(v);

    }

    memset (memo , -1 , sizeof memo);

    int sol = INT_MIN;

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

        sol = max(sol , dp(u));

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

}


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

#define ll long long

#define ull unsigned long long

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

#define endl '\n'

#define main() int main()

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

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

#define pi acos(-1.0)

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

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

#define pb push_back

#define binarycount __builtin_popcount(x)

#define tr t[root]

#define lson t[root << 1]

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

#define fir first

#define sec second

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

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

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

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

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

#define pii pair<string,int>

using namespace std;

#define maxn 100005

const int inf = 0x3f3f3f3f;

const int mod = 9973;

const double eps = 1e-8;

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

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

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

ll powmod(ll a,ll b){

    ull ans = 1, pow = a%mod;

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

    return ans%mod;

}



struct Edge

{

    int to, next, val;

} e[maxn << 1];

int head[maxn], tot = 0;

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

int n,m,ans;

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

void init()

{

    ms(head, -1);

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

}

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

{

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

}



int dfs(int x)

{

	if(!c[x]) return 0;

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

	ll tt=0;

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

    {

    	int y=e[i].to;

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

    }

    return dp[x]=tt;

}

main()

{

	cin>>n>>m;

	init();

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

	{

		int x,y;

		cin>>x>>y;

		add(x,y,1);

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

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

	}

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

	{

		if(!d[i]){

			dfs(i);

		}

	}

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

	{

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

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

	}

	cout<<ans<<endl;

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

#include<cstdio>

using namespace std;

const int maxn = 1e5 + 5;



queue<int>que;

vector<int>vec[maxn];

int in[maxn],ans[maxn];



int main(){

	int n, m;

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

	int u,v;

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

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

		vec[u].push_back(v);

		in[v]++;

	}

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

		if(in[i] == 0){

			que.push(i);

			ans[i] = 0;

		}

	}

	int now;

	while(!que.empty()){

		now = que.front();que.pop();

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

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

			in[vec[now][i]]--;

			if(in[vec[now][i]] == 0){

				que.push(vec[now][i]);

				ans[vec[now][i]] = ans[now] + 1;

			}

		}

	}

	int cnt = 0;

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

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

}

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

#define ll long long

#define ull unsigned long long

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

#define endl '\n'

#define main() int main()

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

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

#define pi acos(-1.0)

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

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

#define pb push_back

#define binarycount __builtin_popcount(x)

#define tr t[root]

#define lson t[root << 1]

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

#define fir first

#define sec second

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

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

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

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

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

#define pii pair<string,int>

using namespace std;

#define maxn 100005

const int inf = 0x3f3f3f3f;

const int mod = 9973;

const double eps = 1e-8;

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

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

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

ll powmod(ll a,ll b){

    ull ans = 1, pow = a%mod;

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

    return ans%mod;

}



struct Edge

{

    int to, next, val;

} e[maxn << 1];

int head[maxn], tot = 0;

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

int n,m,ans;

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

void init()

{

    ms(head, -1);

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

}

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

{

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

}



int dfs(int x)

{

	if(!c[x]) return 0;

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

	ll tt=0;

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

    {

    	int y=e[i].to;

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

    }

    return dp[x]=tt;

}

main()

{

	cin>>n>>m;

	init();

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

	{

		int x,y;

		cin>>x>>y;

		add(x,y,1);

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

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

	}

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

	{

		if(!d[i]){

			dfs(i);

		}

	}

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

	{

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

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

	}

	cout<<ans<<endl;

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

#include<iostream>

#include<algorithm>

#include<queue>

#include<cstring>

#include<vector>

#define inf 0x3f3f3f3f

using namespace std;

typedef long long LL;

const int maxn = 1e5+5;



vector<int>vec[maxn];

queue<int>q;

int in[maxn],num[maxn];



int main(){	

	int n,m;

	scanf("%d",&n);

	scanf("%d",&m);

	for(int i = 0; i <= n; i++) in[i] = 0;

	int u,v;

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

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

		vec[u].push_back(v);

		in[v]++;

	}

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

		if(in[i] == 0) q.push(i);

	}

	int ans = 0;

	while(!q.empty()){

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

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

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

			if(--in[vec[now][i]] == 0){

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

				num[vec[now][i]] = num[now] + 1;

				ans = max(ans, num[vec[now][i]]);

			}

		}

	}

	printf("%d",ans);

}

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

#define ll long long

#define ull unsigned long long

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

#define endl '\n'

#define main() int main()

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

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

#define pi acos(-1.0)

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

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

#define pb push_back

#define binarycount __builtin_popcount(x)

#define tr t[root]

#define lson t[root << 1]

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

#define fir first

#define sec second

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

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

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

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

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

#define pii pair<string,int>

using namespace std;

#define maxn 100005

const int inf = 0x3f3f3f3f;

const int mod = 9973;

const double eps = 1e-8;

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

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

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

ll powmod(ll a,ll b){

    ull ans = 1, pow = a%mod;

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

    return ans%mod;

}



struct Edge

{

    int to, next, val;

} e[maxn << 1];

int head[maxn], tot = 0;

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

int n,m,ans;

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

void init()

{

    ms(head, -1);

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

}

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

{

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

}



int dfs(int x)

{

	if(!c[x]) return 0;

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

	ll tt=0;

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

    {

    	int y=e[i].to;

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

    }

    return dp[x]=tt;

}

main()

{

	cin>>n>>m;

	init();

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

	{

		int x,y;

		cin>>x>>y;

		add(x,y,1);

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

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

	}

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

	{

		if(!d[i]){

			dfs(i);

		}

	}

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

	{

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

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

	}

	cout<<ans<<endl;

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

using namespace std;



int N, M, a, b, ans;

vector<int> V[100100];

int dp[100100];



int f(int a) {

	if (V[a].size() == 0) return 0;

	if (dp[a] != -1) return dp[a];

	dp[a] = 0;

	for(int i=0; i<V[a].size(); i++) {

		int b = V[a][i];

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

	}

	return dp[a];

}



int main() {

	cin >> N >> M;

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

		cin >> a >> b;

		V[a].push_back(b);

	}

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

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

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

	}

	cout << ans << endl;

	return 0;

}

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

using namespace std;



int n, m;

vector<int> edges[100100];

bool vis[100100];

int dp[100100];



int dfs( int node ) {

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

  vis[node] = 1;

  int res = 0;

  for (auto el: edges[node])

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

  return dp[node] = res;

}



int main() {

  ios::sync_with_stdio(false);

  cin >> n >> m;



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

    int u, v;

    cin >> u >> v;

    edges[u].push_back(v);

  }



  int ans = 0;

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

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

  }



  cout << ans << endl;

  return 0;

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

using namespace std;

#define N (int)2e5

#define fi first

#define se second

#define mp make_pair

#define pb push_back

int n,m,memo[N];

vector <int> adj[N];



int dp(int u) {

    if (adj[u].size() == 0) return 0;

    int &sol = memo[u];

    if (sol != -1) return sol;

    int keep = INT_MIN;

    for (int v : adj[u]) {

        keep = max(

            keep ,

            dp(v) + 1

        );

    }

    return sol = keep;

}



int main() {

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

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

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

        adj[u].pb(v);

    }

    memset (memo , -1 , sizeof memo);

    int sol = INT_MIN;

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

        sol = max(sol , dp(u));

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

}


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

using namespace std;



int n, m;

vector<int> edges[100100];

bool vis[100100];

int dp[100100];



int dfs( int node ) {

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

  vis[node] = 1;

  int res = 0;

  for (auto el: edges[node])

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

  return dp[node] = res;

}



int main() {

  ios::sync_with_stdio(false);

  cin >> n >> m;



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

    int u, v;

    cin >> u >> v;

    edges[u].push_back(v);

  }



  int ans = 0;

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

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

  }



  cout << ans << endl;

  return 0;

}
// Slower C++ solution:
//#pragma GCC target ("avx2,sse2")

//#pragma GCC optimization ("Ofast")

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

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



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

#define ll long long

#define ull unsigned long long

#define db long double

#define pb push_back

#define pf push_front

#define ppb pop_back

#define ppf pop_front

#define fi first

#define se second

#define mp make_pair

#define up_b upper_bound

#define low_b lower_bound

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

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

#define endl "\n"



#define left fsdsdfoisf

#define sum dpsdfioppsf

#define assign xcvjlkdjfio

#define trie fksdfkjkfnjuiv

#define next sidlfjsfkl

#define merge sdfksdkfsldf



//#define int long long



using namespace std;



void dout() {

    cerr << endl;

}

template <typename Head, typename... Tail>

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

    cerr << H << ' ';

    dout(T...);

}



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

typedef pair <int, int> pii;



const int N = 1e5 + 7;

int n, m, dp[N];

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

bool used[N];



void dfs(int v) {

    used[v] = true;

    for (auto i : q[v]) {

        if (!used[i]) {

            dfs(i);

        }

    }

    ord.pb(v);

}



void solve(int tc) {

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

    cin >> n >> m;

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

        int x, y;

        cin >> x >> y;

        q[x].pb(y);

        r[y].pb(x);

    }

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

        if (!used[i]) {

            dfs(i);

        }

    }

    reverse(all(ord));

    int res = 0;

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

        int v = ord[i - 1];

        for (auto j : r[v]) {

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

        }

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

    }

    cout << res;

}



signed main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

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

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

    int tc = 1;

//    cin >> tc;

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

        solve(i);

//        cleanup();

    }

}


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

using namespace std;



int n, m;

vector<int> edges[100100];

bool vis[100100];

int dp[100100];



int dfs( int node ) {

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

  vis[node] = 1;

  int res = 0;

  for (auto el: edges[node])

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

  return dp[node] = res;

}



int main() {

  ios::sync_with_stdio(false);

  cin >> n >> m;



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

    int u, v;

    cin >> u >> v;

    edges[u].push_back(v);

  }



  int ans = 0;

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

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

  }



  cout << ans << endl;

  return 0;

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

using namespace std;

#define N (int)2e5

#define fi first

#define se second

#define mp make_pair

#define pb push_back

int n,m,memo[N];

vector <int> adj[N];



int dp(int u) {

    if (adj[u].size() == 0) return 0;

    int &sol = memo[u];

    if (sol != -1) return sol;

    int keep = INT_MIN;

    for (int v : adj[u]) {

        keep = max(

            keep ,

            dp(v) + 1

        );

    }

    return sol = keep;

}



int main() {

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

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

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

        adj[u].pb(v);

    }

    memset (memo , -1 , sizeof memo);

    int sol = INT_MIN;

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

        sol = max(sol , dp(u));

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

}


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

using namespace std;

typedef long long ll;

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

#define INF 1e18

int n, m;

vector<int> graph[100020];

vector<int> dp(100020, -1);

vector<bool> visited(100020, false);

int dfs(int node, int sum)

{

    if(dp[node] != -1)

        return dp[node];

    if(graph[node].empty())

        return 1;

    else{

        int temp, max = 0;

        for (auto child : graph[node]) {

            temp = dfs(child, sum+1);

            if(temp > max)

                max = temp;

        }

        return dp[node] = max+1;

    }

}

int check()

{

    int mx = 0;

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

        if(!visited[i])

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

    }

    return mx-1;

}

int main() {

    fast;

    cin>>n>>m;

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

        int a, b;

        cin>>a>>b;

        graph[a].push_back(b);

    }

    cout<<check();

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

using namespace std;



int N, M, a, b, ans;

vector<int> V[100100];

int dp[100100];



int f(int a) {

	if (V[a].size() == 0) return 0;

	if (dp[a] != -1) return dp[a];

	dp[a] = 0;

	for(int i=0; i<V[a].size(); i++) {

		int b = V[a][i];

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

	}

	return dp[a];

}



int main() {

	cin >> N >> M;

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

		cin >> a >> b;

		V[a].push_back(b);

	}

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

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

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

	}

	cout << ans << endl;

	return 0;

}

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

using namespace std;

#define f first

#define s second

#define ll long long

const int N = 1e5+10;

vector < int > graph[N] ;

int vis[N] , n , mx1  ,m;

map < int , int > mp ;

int dfs ( int node ){

    int mx = 0 ;

    for (auto i : graph[node]){

        if (!vis[i]) {

            vis[i] = 1 ;

            mp[i] = dfs(i) ;

            mx = max(mx,mp[i]+1);

        }

        else

            mx = max(mx,1+mp[i]);

    }

    mp[node] = mx ;

    return mp[node] ;

}

int main()

{

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

    cin >> n >> m ;

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

        int u , v ;

        cin >> u >> v ;

        graph[u].push_back(v) ;

    }

    int mx = 0 ;

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

        if (!vis[i]){

            vis[i] = 1 ;

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

        }

    }

    cout << mx ;

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

using namespace std;

#define N (int)2e5

#define fi first

#define se second

#define mp make_pair

#define pb push_back

int n,m,memo[N];

vector <int> adj[N];



int dp(int u) {

    if (adj[u].size() == 0) return 0;

    int &sol = memo[u];

    if (sol != -1) return sol;

    int keep = INT_MIN;

    for (int v : adj[u]) {

        keep = max(

            keep ,

            dp(v) + 1

        );

    }

    return sol = keep;

}



int main() {

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

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

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

        adj[u].pb(v);

    }

    memset (memo , -1 , sizeof memo);

    int sol = INT_MIN;

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

        sol = max(sol , dp(u));

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

}


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

using namespace std;

#define f first

#define s second

#define ll long long

const int N = 1e5+10;

vector < int > graph[N] ;

int vis[N] , n , mx1  ,m;

map < int , int > mp ;

int dfs ( int node ){

    int mx = 0 ;

    for (auto i : graph[node]){

        if (!vis[i]) {

            vis[i] = 1 ;

            mp[i] = dfs(i) ;

            mx = max(mx,mp[i]+1);

        }

        else

            mx = max(mx,1+mp[i]);

    }

    mp[node] = mx ;

    return mp[node] ;

}

int main()

{

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

    cin >> n >> m ;

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

        int u , v ;

        cin >> u >> v ;

        graph[u].push_back(v) ;

    }

    int mx = 0 ;

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

        if (!vis[i]){

            vis[i] = 1 ;

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

        }

    }

    cout << mx ;

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

using namespace std;

typedef long long ll;

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

#define INF 1e18

int n, m;

vector<int> graph[100020];

vector<int> dp(100020, -1);

vector<bool> visited(100020, false);

int dfs(int node, int sum)

{

    if(dp[node] != -1)

        return dp[node];

    if(graph[node].empty())

        return 1;

    else{

        int temp, max = 0;

        for (auto child : graph[node]) {

            temp = dfs(child, sum+1);

            if(temp > max)

                max = temp;

        }

        return dp[node] = max+1;

    }

}

int check()

{

    int mx = 0;

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

        if(!visited[i])

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

    }

    return mx-1;

}

int main() {

    fast;

    cin>>n>>m;

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

        int a, b;

        cin>>a>>b;

        graph[a].push_back(b);

    }

    cout<<check();

}

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

using namespace std;

#define f first

#define s second

#define ll long long

const int N = 1e5+10;

vector < int > graph[N] ;

int vis[N] , n , mx1  ,m;

map < int , int > mp ;

int dfs ( int node ){

    int mx = 0 ;

    for (auto i : graph[node]){

        if (!vis[i]) {

            vis[i] = 1 ;

            mp[i] = dfs(i) ;

            mx = max(mx,mp[i]+1);

        }

        else

            mx = max(mx,1+mp[i]);

    }

    mp[node] = mx ;

    return mp[node] ;

}

int main()

{

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

    cin >> n >> m ;

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

        int u , v ;

        cin >> u >> v ;

        graph[u].push_back(v) ;

    }

    int mx = 0 ;

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

        if (!vis[i]){

            vis[i] = 1 ;

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

        }

    }

    cout << mx ;

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

using namespace std;

#define mx 200005

vector<int>adj[mx];

bool vis[mx];

int dp[mx];



void dfs(int src){

vis[src] = 1;



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

    if(!vis[adj[src][i]])

        dfs(adj[src][i]);

dp[src] = max(dp[src],1 + dp[adj[src][i]]);

}

}

int longest_path(int n){

    int ans = 0;

    memset(dp,0,sizeof dp);

    memset(vis,0,sizeof vis);



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

        if(!vis[i])

            dfs(i);

    }

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

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

    }

return ans;

}









int main(){

int n,m;

cin>>n>>m;

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

    int x,y;

    cin>>x>>y;

    adj[x].push_back(y);

}



cout<<longest_path(n)<<endl;

}


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

using namespace std;

#define f first

#define s second

#define ll long long

const int N = 1e5+10;

vector < int > graph[N] ;

int vis[N] , n , mx1  ,m;

map < int , int > mp ;

int dfs ( int node ){

    int mx = 0 ;

    for (auto i : graph[node]){

        if (!vis[i]) {

            vis[i] = 1 ;

            mp[i] = dfs(i) ;

            mx = max(mx,mp[i]+1);

        }

        else

            mx = max(mx,1+mp[i]);

    }

    mp[node] = mx ;

    return mp[node] ;

}

int main()

{

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

    cin >> n >> m ;

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

        int u , v ;

        cin >> u >> v ;

        graph[u].push_back(v) ;

    }

    int mx = 0 ;

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

        if (!vis[i]){

            vis[i] = 1 ;

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

        }

    }

    cout << mx ;

}