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

#include<algorithm>

#include<iomanip>

#include<cmath>

#include<vector>

#include<queue>

#include<stack>

#include<set>

#include <cstdio>

#include<map>

#include<string>

#include<assert.h>

#include<unordered_map>

#include <cstring>//memset();

#define Amr_Abdelazim ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#define pb push_back

#define pp pop_back

#define ll long long

#define ld long double

#define pll pair<ll,ll> 

#define vll vector<ll> 

#define vvll vector<vector<ll> >  

#define vpll vector<pair<ll,ll> > 

#define all(v) v.begin(),v.end()

#define rep(i,n)   for(int i=0;i<n;i++)

#define repp(s,i,n)   for(int i=s;i<n;i++)

#define print(v) for(auto i:v)cout<<i<<" ";cout<<"\n"; 

#define mkp make_pair

#define clr(ar,v) memset(ar,v,sizeof(ar));

#define TC ll t;cin>>t;while(t--)

#define in_vec ll n;cin>>n;vll v(n);rep(i,n)cin >> v[i];

#define EPS 1e-9

using namespace std;

const int mod = 1000000007;

const int N = 1000005;

class cmp{

public:

	bool operator() (pair<ll, pll> &A, pair<ll, pll> &B){

		return A > B;

	}

};

bool by_sec(pll &A, pll &B){

	return A.second < B.second;

}

bool is(pll a, pll b){

	return !((a.first > b.second&&a.second > b.second) || (a.first < b.first&&a.second < b.first));

}

ld dist(pll p1, pll p2){

	return (ld)sqrt((ld)(p1.first - p2.first)*(p1.first - p2.first) + (ld)(p1.second - p2.second)*(p1.second - p2.second));

}

vvll vv(100000+9);

ll dp[100000 + 9];

ll path(ll n){

	if (vv[n].size() == 0)return 0;

	if (dp[n] == -1){

		ll mx = -1;

		rep(i, vv[n].size())mx = max(mx, path(vv[n][i])+1);

		dp[n] = mx;

	}

	return dp[n];

}

int main()

{

	Amr_Abdelazim

		clr(dp, -1);

		ll n, m,ans=0;

	cin >> n >> m;

	while (m--){

		ll a, b;

		cin >> a >> b;

		vv[a].push_back(b);

	}

	repp(1, i, n + 1)

		ans = max(ans, path(i));

	cout << ans << "\n";

	return 0;

}

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

#define ll long long

#define fi first

#define se second

using namespace std;

typedef pair<ll,ll> lp;

const int inf=0x3f3f3f3f;

void A() {

    int n;

    cin>>n;

    vector<int> a(n,0);

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

        cin>>a[i];

    vector<int> dp(n+1,inf);

    dp[0]=0;

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

        dp[i]=min(dp[i],dp[i-1]+abs(a[i]-a[i-1]));

        if(i>=2)

            dp[i]=min(dp[i],dp[i-2]+abs(a[i]-a[i-2]));

    }

    cout<<dp[n-1]<<endl;



}

void B() {

    int n,k;

    cin>>n>>k;

    vector<int> dp(n+1,inf);

    vector<int> h(n,0);

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

        cin>>h[i];

    dp[0]=0;

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

        for(int len=1; len<=min(k+1,i+1); len++) {

            int j=i-len+1;

            dp[i]=min(dp[i],dp[j]+abs(h[j]-h[i]));

        }

    }

    cout<<dp[n-1]<<endl;

}

void C() {

    int n;

    cin>>n;

    vector<vector<int>> a(n,vector<int>(3,0));

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

        cin>>a[i][0]>>a[i][1]>>a[i][2];

    vector<vector<int>> dp(n,vector<int>(3,0));

    dp[0][0]=a[0][0];

    dp[0][1]=a[0][1];

    dp[0][2]=a[0][2];

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

        for(int j=0; j<3; j++) {

            for(int k=0; k<3; k++) {

                if(j==k)

                    continue;

                dp[i][j]=max(dp[i][j],dp[i-1][k]+a[i][j]);

                //cout<<dp[i][j]<<endl;

            }

        }

    }

    cout<<max(dp[n-1][0],max(dp[n-1][1],dp[n-1][2]))<<endl;

}

void D() {

    ll n,W;

    cin>>n>>W;

    vector<lp> p;

    for(int i=0; i<n; i++) {

        ll a,b;

        cin>>a>>b;

        p.emplace_back(a,b);

    }

    sort(p.begin(),p.end());

    vector<ll> dp(W+1,0);

    for(int i=0; i<n; i++) {

        for(int j=W; j>=p[i].first; j--) {

            if(j>0)

                dp[j]=max(dp[j],dp[j-1]);

            dp[j]=max(dp[j],dp[j-p[i].fi]+p[i].se);

        }

    }

    cout<<dp[W]<<endl;



}

void E() {

    //价值比较小，将dp[i]转化为获得当前i的价值的最小体积，然后根据W寻找符合要求的解；

    int n,W;

    cin>>n>>W;

    vector<ll> w(n,0);

    vector<ll> v(n,0);

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

        cin>>w[i]>>v[i];

    vector<ll> dp(100005,inf);

    dp[0]=0;

    int res=0;

    for(int i=0; i<n; i++) {

        for(int j=1e5; j>=v[i]; j--) {

            dp[j]=min(dp[j],dp[j-v[i]]+w[i]);

            if(dp[j]<=W) {

                res=max(res,j);

            }

        }

    }

    cout<<res<<endl;



}

void F() {

    string s,t;

    cin>>s>>t;

    int n=s.size(),m=t.size();

    vector<vector<int>> dp(n+1,vector<int>(m+1,0));

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

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

            char a=s[i-1],b=t[j-1];

            if(a==b) {

                dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);

            }

            dp[i][j]=max(dp[i][j],max(dp[i-1][j],dp[i][j-1]));

        }

    }

    int i=n,j=m;

    stack<char> st;

    while(i!=0&&j!=0) {

        char a=s[i-1],b=t[j-1];

        if(a==b) {

            st.push(a);

            j--;

            i--;

        } else {

            if(dp[i][j]==dp[i-1][j])

                i--;

            else

                j--;

        }

    }

    string res;

    while(!st.empty()) {

        res+=st.top();

        st.pop();

    }

    cout<<res<<endl;

}

int Ghelper(vector<int>& dp,vector<vector<int>>& edge,int cur,int& ans)

{

    if(dp[cur]!=-1) return dp[cur];

    int res=0;

    for(auto nb:edge[cur]){

        res=max(res,Ghelper(dp,edge,nb,ans)+1);

    }

    ans=max(ans,res);

    dp[cur]=res;

    return res;

}

void G()

{

    int n,m;

    cin>>n>>m;

    vector<vector<int>> edge(n+1,vector<int>());

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

        int x,y;

        cin>>x>>y;

        edge[x].push_back(y);

    }

    vector<int> dp(n+1,-1);

//    vector<bool> vis(n+1,0);

    int res=0;

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

//        if(vis[i]) continue;

//        vis[i]=true;

        Ghelper(dp,edge,i,res);

    }

    cout<<res<<endl;

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

    G();

}