// 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.8585070777949206x):
#include <iostream>

#include <vector>

#include <cmath>

#include <set>

#include <queue>

#include <map>

#include <cstdio>

#include <cstring>

#include <string>

#include <stack>

#include <algorithm>

#include <limits.h>

#include <bits/stdc++.h>

#define fw(x) freopen("x.txt","w",stdout)

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

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

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

#define mem(a,s) memset(a,s,sizeof a)

#define fast ios_base::sync_with_stdio(0);cin.tie(NULL)

#define pf printf

#define sc scanf

#define ll long long

#define pii pair<int,int>

#define pll pair<ll,ll>

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

#define vii vector<int>

#define vll vector<ll>

#define vss vector<string>

#define debug(x)  cout<<#x"="<<(x)<<endl

#define pb push_back





using namespace std;





// moves



//int dx[]= {0,0,1,-1};/*4 side move*/

//int dy[]= {-1,1,0,0};/*4 side move*/

//int dx[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/

//int dy[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/

//int dx[]={1,1,2,2,-1,-1,-2,-2};/*knight move*/

//int dy[]={2,-2,1,-1,2,-2,1,-1};/*knight move*/





//big_mod



//ll bigmod(ll a,ll b,ll m)

//{if(b == 0) return 1%m;ll x = bigmod(a,b/2,m);x = (x * x) % m;if(b % 2 == 1) x = (x * a)% m;return x;}

//ll BigMod(ll B,ll P,ll M){ ll R=1%M; while(P>0) {if(P%2==1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return R;} /// (B^P)%M



//ll getBit(ll num, int idx) {return ((num >> idx) & 1ll) == 1ll;}

//ll setBit1(ll num, int idx) {return num or (1ll<<idx);}

//ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}

//ll flipBit(ll num, int idx) {return num ^ (1ll<<idx);}



const int mod = 1000000007;

int n, m, x, y;

vii gr[100001];

int len[100001];

int ans=0;

int dp(int cur){

  if(len[ cur ] == -1){

    int sz = gr[cur].size();

    if(sz>0){

      int mx = 0;

      for(int j=0; j<sz; j++){

        if(len[ gr[cur][j] ] == -1) {

          len[gr[cur][j]] = dp(gr[cur][j]);

          mx = max(mx, len[gr[cur][j]]);

        }

        else mx = max(mx, len[gr[cur][j]]);

      }

      len[cur] = mx+1;

      return len[cur];

    }

    else return 0;

  }

  else return len[cur];

}

int main()

{

    fast;

    mem(len, -1);

    cin>>n>>m;

    REP1(i, m){

      cin>>x>>y;

      gr[x].pb(y);

    }

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

        int sz=gr[i].size();

        if(sz>0){

          int mx=0;

          for(int j=0; j<sz; j++){

            if(len[ gr[i][j] ] == -1) {

              len[gr[i][j]] = dp(gr[i][j]);

              mx = max(mx, len[gr[i][j]]);

            }

            else mx = max(mx, len[gr[i][j]]);

          }

          len[i] = mx+1;

          ans = max(ans, mx+1);

        }

        else len[i] = 0;

    }

    cout<<ans<<'\n';

    return 0;

}