//         PAR_YUMMM            //
                                            //           CPP                //
#include <bits/stdc++.h>
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
 
#define ll long long
#define ld long double
ll const N=1e6+5;
 
#define pb push_back
#define popb pop_back()
#define pf push_front
#define popf pop_front()
#define si size()
#define be begin()
#define en end()
#define all(v) v.be, v.en
#define le length()
#define mp make_pair
#define mt make_tuple
#define acc(v) accumulate(all(v), 0)
#define F first
#define S second
 
#define forz(i, n) for (int i = 0; i < n; i++)
#define fore(i, m, n) for (int i = m; i <= n; i++)
#define rforz(i, n) for (int i = n - 1; i >= 0; i--)
#define rfore(i, m, n) for (int i = n; i >= m; i--)
#define deci(n) fixed << setprecision(n)
#define high(n) __builtin_popcount(n)
#define highll(n) __builtin_popcountll(n)
#define parity(n) __builtin_parity(n)
#define ctz(n) __builtin_ctz(n)
#define lb lower_bound
#define ub upper_bound
#define er equal_range
#define maxe *max_element
#define mine *min_element
#define mod 1000000007
#define mod2 998244353
#define gcd __gcd
#define sexy ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
 
#define endl "\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
 
#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define osetlli tree<lli, null_type, less<lli>, rb_tree_tag, tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
#define ofk order_of_key
#define fbo find_by_order
 
ll po(ll x,ll y){
    if(y==0){
        return 1;
    }
    ll temp=po(x,y/2);
    if(y%2==0){
        return ((temp%mod)*(temp%mod))%mod;
    }else{
        return (((x%mod)*(temp%mod))%mod*(temp)%mod)%mod;
    }
}
 
ll lcm(ll a, ll b){ 
    return (a*b)/(gcd(a, b));
}
ll max(ll a, ll b){
  if (a > b)
    return a;
  return b;
}
ll min(ll a, ll b){
  if (a < b)
    return a;
  return b;
}
 
ll bins(ll ar[],ll l,ll r,ll x) 
{ 
    if (r >= l) { 
        ll mid =l+((r-l)/2); 
        if(ar[mid]==x) 
            return mid; 
        if(ar[mid]>x) 
            return bins(ar,l,mid-1,x);
        return bins(ar,mid+1,r,x); 
    } 
    return -1; 
} 

vector <vector<ll>> adj(N),gr(N);
ll deg[N];
vector <ll> topo;
bool vis[N];
void dfs(ll x){
    vis[x]=true;
    for(auto it:adj[x]){
        if(!vis[it]){
            dfs(it);
        }
    }
    topo.pb(x);
}

int main(){
    sexy;
    ll x,y,z,p,q,r;
    cin>>x>>y;
    ll ar[x+1];
    forz(i,x+1){
        ar[i]=0;
    }
    forz(i,y){
        cin>>p>>q;
        deg[q]++;
        adj[p].pb(q);
        gr[q].pb(p);
    }
    fore(i,1,x){
        if(!vis[i]){
            dfs(i);
        }
    }
    reverse(all(topo));
    for(auto it:topo){
        for(auto i:gr[it]){
            ar[it]=max(ar[it],1+ar[i]);
        }
    }
    z=0;
    fore(i,1,x){
        z=max(z,ar[i]);
    }
    p1(z);
    return 0;
}


Measured memory_bytes: 58236