//RITIK RAI:
//#pragma GCC optimize("O3")
 
/*----------------------------------------------------------------------------------------------------------*/
 
#include<bits/stdc++.h>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<iterator>
 
using namespace std;
 
/*----------------------------------------------------------------------------------------------------------*/
 
#define ll long long int
#define ull unsigned long long int
#define ld long double
 
/*----------------------------------------------------------------------------------------------------------*/
 
#define vll vector<ll>
#define vvll vector<vector<ll>>
 
#define vstr vector<string>
 
#define vi vector<int>
#define vvi vector<vector<int>>
 
#define pb push_back
 
/*----------------------------------------------------------------------------------------------------------*/
 
#define mll map<ll,ll>
 
/*----------------------------------------------------------------------------------------------------------*/
 
#define pll pair<ll,ll>
#define mp make_pair
#define fir first
#define sec second
 
/*----------------------------------------------------------------------------------------------------------*/
 
#define all(x) x.begin(),x.end()
#define ub upper_bound
#define lb lower_bound
#define bin binary_search
 
/*----------------------------------------------------------------------------------------------------------*/
 
#define vmin *min_element
#define vmax *max_element
#define intg INT_MAX
#define ints INT_MIN
 
/*----------------------------------------------------------------------------------------------------------*/
 
#define mod 1000000007
#define endl '\n'
 
/*----------------------------------------------------------------------------------------------------------*/
 
#define fill0(arr,a,b) for(ll i=a;i<b;i++) arr[i]=0
 
/*----------------------------------------------------------------------------------------------------------*/
 
#define f(i,a,b) for(ll i=a;i<b;i++)
#define rf(i,a,b) for(ll i=a;i>b;i--)
 
/*----------------------------------------------------------------------------------------------------------*/
 
#define print1(arr,a,b) for(ll i=a;i<b;i++) cout<<arr[i]<<" "; cout<<endl
#define print(arr,a,b)  for(ll i=a;i<b;i++) cout<<arr[i]<<endl
#define print2d(dp,r,c) for(ll i=0;i<r;i++){for(ll j=0;j<c;j++) cout<<dp[i][j]<<" "; cout<<endl;}
 
/*----------------------------------------------------------------------------------------------------------*/
 
#define ritik ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
 
 
 
 
/*--------------------------------------------FUNCIONS--------------------------------------------------------------*/
 
 
/*
    ll p=1000000007;
        fac[0] = 1;
        f(i,1,100005)
            fac[i] = (fac[i-1]*i)%p;
*/
 
 
ll power(ll x,ll y,ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
 
ll max3(ll a,ll b,ll c)
{
    return max(max(a,b),c);
}
 
 
/*------------------------------------------GLOBAL VARIABLES----------------------------------------------------------------*/
 
#define maxm 100003
ll fac[300007];
 
/*-----------------------------------------------write your code here-----------------------------------------------------------*/
 
vll graph[maxm];
vll ans(maxm,-1);
ll mx=0;
ll dfs(ll r)
{
    if(ans[r]!=-1) return(ans[r]);
    ll tmp=0;
    f(i,0,graph[r].size())
        tmp=max(tmp,dfs(graph[r][i])+1);
    ans[r]=tmp;
    mx=max(mx,tmp);
    return(ans[r]);
}
 
int main()
{
    ll n,m; cin>>n>>m;
    f(i,0,m)
    {
        ll x,y; cin>>x>>y;
        graph[x].pb(y);
    }
    f(i,1,n+1) dfs(i);
    cout<<mx<<endl;
}

Measured memory_bytes: 10240