// Slower C++ solution:
#include<iostream>

#include<iomanip>

#include<cmath>

#include<string>

#include<cctype>

#include<algorithm>

#include<set>

#include<map>

#include<stack>

#include<queue>

#include<cstring>

//#include<windows.h>

//#include<time.h>

#define pb push_back

#define ppb pop_back

#define fi first

#define se second

#define mer make_pair

#define mme memset

using namespace std;



const double pie1=3.1415926;//π1

const double pie2=3.14159265358979;//π2

const double pie3=3.14159265358979323846;//π3

const int vbi=999999999;

const int vli=-vbi;

const long long vbl=999999999999999999;

const long long vll=-vbl;



typedef long long ll;

typedef pair<int,int> pii;

typedef pair<ll,ll> pll;

typedef pair<pii,int> piii;

typedef pair<pii,pii> pi4;

typedef pair<pll,ll> plll;

typedef pair<int,pii> ipii;

typedef pair<ll,pll> lpll;

typedef pair<pll,pll> pl4;

typedef vector<int> vi;

typedef vector<ll> vl;

typedef vector<pii> vpi;

typedef vector<pll> vpl;

typedef set<int> sti;

typedef set<ll> stl;

typedef set<pii> spi;

typedef set<pll> spl;

priority_queue<int> pqi;

priority_queue<ll> pql;

queue<int> qi;

queue<ll> ql;



/*void bin_v(){

	char bin[1000];

	gets(bin);

}*/



bool pss(int a){

	for(int i=2;i<=sqrt(a);i++)

		if(a%i==0)

			return false;

	return true;

}

/////////////////////////////////////////////////////////////////////////////////////////

int n,m;

vector<int> v[100010];

int d[100010];

int dp[100010];



int main(){

	//my name is Veex_WCY

	ios::sync_with_stdio(false);

	cin>>n>>m;

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

		int a,b;

		cin>>a>>b;

		v[a].pb(b);

		d[b]++;

	}

	stack<int> st;

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

		if(d[i]==0)

			st.push(i);

	while(!st.empty()){

		int x=st.top();

		st.pop();

		for(int i=0;i<v[x].size();i++){

			int ne=v[x][i];

			if(dp[x]+1>dp[ne])

				dp[ne]=dp[x]+1;

			d[ne]--;

			if(d[ne]==0)

				st.push(ne);

		}

	}

	int maxn=0;

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

		maxn=max(maxn,dp[i]);

	cout<<maxn;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.48493859563114x):
#include<bits/stdc++.h>

#define ll          long long int

#define ld          long double

#define pb          push_back

#define mp          make_pair

#define ff          first

#define se          second

#define scan(n)     scanf("%lld", &n);

#define print(n)    printf("%lld\n", n);

#define fd(n)       fixed<<setprecision(n)

#define MAX         1000000000000000000

#define MAXN        100005

#define LOG         19

#define mod         1000000007

using namespace std;





int main(){

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    ll n,m,i,x,y,mx=0;

    cin>>n>>m;

    vector<ll> vi[n+1];

    ll deg[n+1];

    queue<ll> qe;

    vector<ll> vi1;

    memset(deg,0,sizeof(deg));

    for(i=1;i<=m;i++){

        cin>>x>>y;

        vi[x].pb(y);

        deg[y]++;

    }

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

        if(deg[i]==0){

            qe.push(i);

            vi1.pb(i);

        }

    }

    while(!qe.empty()){

        ll node=qe.front();

        qe.pop();

        for(auto x:vi[node]){

            deg[x]--;

            if(deg[x]==0){

                vi1.pb(x);

                qe.push(x);

            }

        }

    }

    ll dp[n+1];

    memset(dp,0,sizeof(dp));

    for(auto node:vi1){

        for(auto x:vi[node]){

            dp[x]=max(dp[x],dp[node]+1);

        }

    }

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

        mx=max(mx,dp[i]);

    }

    cout<<mx;

}