// 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: 1.1165099914905114x):
#include <bits/stdc++.h>



#define Fast cin.tie(0), ios::sync_with_stdio(0)

#define All(x) x.begin(), x.end()

#define louisfghbvc int t; cin >> t; while(t--)

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

using namespace std;

typedef long long LL;

typedef pair<LL, LL> ii;

typedef vector<LL> vi;



template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep = ""; for(const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }



void dbg_out() { cerr << "\n"; }

template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }



const int N = 1e5+5;

vector<int> g[N];

bool vis[N];

int dp[N];



void dfs(int u){

    for(int v: g[u]){

        if(!vis[v]){

            vis[v] = 1;

            dfs(v);

        }

        dp[u] = max(dp[u], dp[v]+1);

    }

}



void solve(){

    int n, m;

    cin >> n >> m;

    for(int i = 1; i <= n; ++i) g[i].clear();

    memset(dp, 0, sizeof dp);

    memset(vis, 0, sizeof vis);



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

        int a, b;

        cin >> a >> b;

        g[a].push_back(b);

    }



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

        if(!vis[i]){

            vis[i] = 1;

            dfs(i);

        }

    }



    cout << *max_element(dp+1, dp+n+1) << "\n";

}



int main()

{

    Fast;

    //louisfghbvc{

        solve();

    //}

    return 0;

}