// Slower C++ solution:
#define _CRT_SECURE_NO_WARNINGS

#include<bits/stdc++.h>

#include<iostream>

#include<algorithm>

#include<iomanip>

#include<string>

#include<cmath>

#include<vector>

#include<set>

#include<queue>

#include<stack>

#include<fstream>

#include<map>

#include<bitset>

#include<unordered_map>

#include<cstring>

#include<cctype>

using namespace std;

#define  ll long long

#define  ull unsigned long long

#define  ul unsigned long

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

#define sz(n) (int)n.size()

#define pb(s) push_back(s);

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

#define finl "\n"

#define Mohammed_Atef_Hassan fast();

#define mod 1000000007

#define INF LLONG_MAX

#define PI acos(-1)

#define clr(dp,n) memset(dp,n,sizeof dp)

#define ex(n) return cout<<n<<finl,0;

int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };

int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };

ll fix_mod(ll x, ll y)

{

	return (y + x % y) % y;

}

void fast() {

	std::ios_base::sync_with_stdio(false);

	cin.tie(nullptr); cout.tie(nullptr);

}

vector<vector<int>>edg;

int mem[(int)1e5 + 10];

int solve(int u)

{

	int& ret = mem[u];

	if (~ret)return ret;

	ret = 0;

	for (auto v : edg[u])

	{

		ret = max(ret,solve(v)+1);

	}

	return ret;

}

int main()

{

	Mohammed_Atef_Hassan

		clr(mem, -1);

		int n, m;

	cin >> n >> m;

	edg = vector<vector<int>>(n + 1);

	while (m--)

	{

		int x, y;

		cin >> x >> y;

		edg[x].push_back(y);

	}

	int mx = 0;

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

	{

		mx = max(mx, solve(i));

	}

	ex(mx);

}

// Faster C++ solution for the same problem (measured speedup: 1.3897362646375724x):
#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();

}