/*
Author: Arham_doshi
@quote "Code like there's no tommorow!"
 ***************************************   UNAUTHORISED COPYING OF CODE PROHIBITED   **********************************
*/
#include <bits/stdc++.h>

#define int         long long
#define double      long double
#define pb          push_back
#define pf          push_front
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        998244353 
#define PI          3.141592653589
#define bs(v,n)     binary_search(all((v)),(n))
#define lb(v,n)     lower_bound(all((v)),(n))
#define ub(v,n)     upper_bound(all((v)),(n))
#define fo(i,l,u)   for(i=l;i<u;i++)//my love
using namespace std;

int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }  
bool mod(double a,double b) {return a/b - floor(a/b);}


int logb(int base, int x) {
    return (log(x) / log(base));
}



//--------------graphs----------------------------------
vector<int> graph[100000];
int vis[100005];

void dfs (int p){
    if(vis[p]!=-1)return;
    int i,j,x;
    vis[p]=0;
    fo(i,0,graph[p].size()){
        x=graph[p][i];
        dfs(x);
        vis[p]=max(vis[x]+1,vis[p]);
        
    }
}



void cgraph(int q,int n){
    int a,b;
    
    memset(vis,-1,sizeof(vis));
    while(q--){
        cin>>a>>b;
        //cout<<a<<" "<<b<<endl;
        graph[a].pb(b);
        
    }
    return ;
}

//---------------------------graphs over-------------------

//--------------------------Dsu---------------------
/*
int ds[100005];
void ds_start(int n){
    int i;
    
    fo(i,0,n+1){
        ds[i]=-1;
        
    }
}
int parent(int x){
    if(ds[x]<0)return x;
    return parent(ds[x]);
}
bool ds_find(int x,int y){
    int p_x,p_y;
    p_x=parent(x);
    p_y=parent(y);
    return (p_x==p_y);
}
int dsw(int x){
    return -1*ds[x];
}
int ds_union(int x,int y){
    int p_x,p_y;
    p_x=parent(x);
    p_y=parent(y);
    
    if(p_x!=p_y){
    if(dsw(p_x)>=dsw(p_y)){
        //cout<<x<<' '<<y<<' '<<ds[p_x]<<' '<<ds[p_y]<<endl;
        ds[p_x]+=ds[p_y];
        ds[p_y]=p_x;
        //cout<<x<<' '<<y<<' '<<ds[p_x]<<' '<<ds[p_y]<<endl;
        return dsw(p_x);
    }
    else {
        
        ds[p_y]+=ds[p_x];
        ds[p_x]=p_y;
        return dsw(p_y);
    }}
    return -1;
}

void ds_show(int n){
   int i;
    fo(i,1,n+1)cout<<ds[i]<<' ';
    cout<<endl;
}
*/

//------------------------------dsu over---------------





//---------------math module----------------
/*int ncr[1000][1000];
//bc means binomial coefficient
void bc(){
    int n=1000,i,j;
    _init0(ncr);
    fo(i,0,n){
        ncr[i][0]=1;
    }
    
    fo(i,1,n){
        fo(j,1,i+1){
            ncr[i][j]=(ncr[i-1][j]+ncr[i-1][j-1])%MOD;
        }
    }
 
}*/

//--------------------math over--------------------





signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int i,j,x,n,p,a,b;
    
    cin>>n>>p;
    cgraph(p,n);
    x=0;
    fo(i,1,n+1){
        if(vis[i]==-1){
            dfs(i);
            x=max(vis[i],x);
            
        }
    }
    
    cout<<x<<endl;
/*fo(i,1,n+1){
    cout<<vis[i]<<" ";
}*/

    return 0;
}

Measured memory_bytes: 8576