// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



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

#define clr(v, d)       memset(v, d, sizeof(v))

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

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

#define lpi(i, j, n)    for(int i=(j); i<(int)(n); ++i)

#define lpd(i, j, n)    for(int i=(j); i>=(int)(n); --i)

#define INF             1000000000





typedef long long ll;

typedef pair<int, int> ii;

typedef vector<ii> vii;

typedef vector<int> vi;



struct edge{

	int from;

	int to;

	int nxt_edge;

};



const int N = 1e5+5;



int n, e;

struct edge edges[N];

int heads[N];

int dp[N];



int solve(int cur_edge_idx){

	if(cur_edge_idx == -1)

		return 0;

// 	cout << cur_edge_idx << endl;

	int & cur = dp[cur_edge_idx];

	if(~cur)

		return cur;

	cur  = solve(edges[cur_edge_idx].nxt_edge);

	if(edges[cur_edge_idx].to > 0)

	    cur = max(cur, 1 + solve(heads[edges[cur_edge_idx].to]));

	return cur;

}



int main()

{

    cin >> n >> e;

	clr(edges, 0);

	clr(heads, -1);

	clr(dp, -1);

    lp(i, e){

    	cin >> edges[i].from >> edges[i].to;

    	edges[i].nxt_edge = heads[edges[i].from];

    	heads[edges[i].from] = i;

    }

    int res = 0;

    lpi(i, 1, n+1)

    	res = max(res, solve(heads[i]));

    cout << res << endl;

    return 0;

}




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

#define ll long long

#define ull unsigned long long

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

#define endl '\n'

#define main() int main()

#define ff1(i,n) for(int i=1;i<=n;++i)

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

#define pi acos(-1.0)

#define ms(a, x) memset(a, x, sizeof(a))

#define lowbit(x) ((x)&(-(x)))

#define pb push_back

#define binarycount __builtin_popcount(x)

#define tr t[root]

#define lson t[root << 1]

#define rson t[root << 1 | 1]

#define fir first

#define sec second

#define debug(x) cout<<x<<'\n'

#define sa(a) scanf("%d", &a)

#define sab(a, b) scanf("%d%d", &a, &b)

#define pa(a) printf("%d",a)

#define pab(a,b) printf("%d%d",a,b)

#define pii pair<string,int>

using namespace std;

#define maxn 100005

const int inf = 0x3f3f3f3f;

const int mod = 9973;

const double eps = 1e-8;

ll gcd(ll a, ll b) {return b==0?a:gcd(b, a%b);}

ll lcm(ll a, ll b) {return a/gcd(a, b)*b;}

int max(int x,int y){if(x>y) return x;return y;}

ll powmod(ll a,ll b){

    ull ans = 1, pow = a%mod;

    while(b) { if(b&1) ans = (ans*pow)%mod; pow = (pow*pow)%mod; b >>= 1; }

    return ans%mod;

}



struct Edge

{

    int to, next, val;

} e[maxn << 1];

int head[maxn], tot = 0;

int dis[maxn], dep[maxn], pre[maxn][21];

int n,m,ans;

int d[maxn],c[maxn],dp[maxn];

void init()

{

    ms(head, -1);

    tot = 0;ms(d,0);ans=0;ms(dp,0);

}

void add(int x, int y, int val)

{

    e[tot].to = y, e[tot].val = val, e[tot].next = head[x], head[x] = tot++;

}



int dfs(int x)

{

	if(!c[x]) return 0;

	if(dp[x])return dp[x];

	ll tt=0;

 	for (int i = head[x]; ~i; i = e[i].next)

    {

    	int y=e[i].to;

        tt=max(tt,dfs(y)+1);

    }

    return dp[x]=tt;

}

main()

{

	cin>>n>>m;

	init();

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

	{

		int x,y;

		cin>>x>>y;

		add(x,y,1);

		if(!d[y])d[y]=1;//d为出度 

		if(!c[x])c[x]=1;

	}

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

	{

		if(!d[i]){

			dfs(i);

		}

	}

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

	{

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

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

	}

	cout<<ans<<endl;

}