#include <bits/stdc++.h>
using namespace std;
#define MODS 100005
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
#define ll long long int
#define ld long double
#define bigint int64_t
#define vll vector<ll>
#define vpll vector<pair<ll,ll>>
#define mp make_pair
#define pb push_back
#define test ll t;read(t);while(t--)
#define pll pair<ll,ll>
#define vvll vector<vector<ll>>
#define fst first
#define scd second
#define ins insert
#define line "\n"

#define forn(i,n) for(ll (i) = 0 ; (i) < (n) ; ++(i))
#define for1(i,n) for(ll (i) = 1 ; (i) <= (n) ; ++(i))
#define forr(i,n) for(ll (i) = (n)-1 ; (i)>=0 ; --(i))
#define forab(i,a,b,c) for(ll (i) = a ; (i) <= (b) ; (i)+=(c))

template <typename T>
void read(T &x){
    cin >> x;
}
template <typename T , typename T0>
void read(T &x,T0 &y){
    cin >> x >> y;
}

template <typename T , typename T0 , typename T1>
void read(T &x,T0 &y,T1 &z){
    cin >> x >> y >> z;
}

template <typename T , typename T0 , typename T1 , typename T2>
void read(T &x,T0 &y,T1 &z,T2 &w){
    cin >> x >> y >> z >> w;
}

template <typename T , typename T0>
void read(pair< T , T0 > &p){
    cin >> p.fst >>p.scd;
}

template <typename T>
void read(vector< T > &oneD,ll n){
    forn(i,n)
    {
        ll x;
        read(x);
        oneD.push_back(x);
    }
}

template <typename T>
void read(T oneD[] , ll n){
    for(ll i=0;i<n;i++){
        read(oneD[i]);
    }
}

template <typename T>
void write(T &x){

    cout << x << " ";
}

template <typename T , typename T0>
void write(T &x,T0 &y){
    cout << x << " " << y << "\n";
}

template <typename T , typename T0 , typename T1>
void write(T &x,T0 &y,T1 &z){
    cout << x << " " << y << " " << z << "\n";
}

template <typename T , typename T0 , typename T1 , typename T2>
void write(T &x,T0 &y,T1 &z,T2 &w){
    cout << x << " " << y << " " << z << " " << w << "\n";
}

template <typename T , typename T0>
void write(pair< T , T0 > &p){
    write(p.fst);
    write(p.scd);
    cout << line;
}

template <typename T>
void write(vector< T > &oneD,ll n){
    for(ll i=0;i<n;i++){
        cout<<oneD[i]<<" ";
    }
    cout << line;
}

template <typename T>
void write(T oneD[] ,int n){
    for(ll i=0;i<n;i++){
        write(oneD[i]);
    }
    cout <<line;
}

template <typename T , typename T0>
void write(map< T , T0 > &mpp){
    for(auto it : mpp){
        write(it.fst);
        cout << ": ";
        write(it.scd);
        cout << "\n";
    }
    cout<<line;
}
void write(vvll rc,ll n,ll m)
{
    forn(i,n)
    {
        forn(j,m)
        {
            cout<<rc[i][j]<<" ";
        }
        cout<<line;
    }
}
vll c[MODS];
ll trap=0;
unordered_map<ll,ll> mymap;
ll maxl(ll a,ll b)
{
    return (a>b)?a:b;
}
/*void bfs(ll x)
{
    queue<pll> q;
    q.push({x,0});
    while(!q.empty())
    {
        pll p=q.front();
        q.pop();
        ll dist=p.scd;
        ll ver=p.fst;
        mark[ver]=dist;
        trap=maxl(trap,dist);
        for(ll i=0;i<c[ver].size();i++)
        {
            ll v=c[ver][i];
            q.push({v,dist+1});
        }
    }
}*/
ll dfs(ll v,ll path)
{
    ll ct=path;
    if(mymap.count(v))
    {
        /*cout<<"Path Found "<<v<<" : "<<mymap[v]<<line;*/
        return mymap[v];
    }
    for(ll i=0;i<c[v].size();i++)
    {
        ll ver=c[v][i];
        ct=maxl(ct,dfs(ver,path));
        /*cout<<"Ver Dist "<<ver<<" : "<<ct<<line;*/
    }
    mymap[v]=ct+1;
    /*cout<<"1st Return "<<v<<" : "<<ct+1<<line;*/
    return ct+1;
}
void solve()
{
    ll n,m;
    read(n,m);
    forn(i,m)
    {
        ll a,b;
        read(a,b);
        c[a].pb(b);
    }
    for1(i,n)
    {
        if(!mymap.count(i))
            trap=maxl(trap,dfs(i,0));
        else
            trap=maxl(trap,mymap[i]);
    }
    cout<<trap-1;
}
int main() {
   /*#ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif*/
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//*******************************************************//
solve();

return 0;
}




Measured memory_bytes: 11940