// Original C++ solution (slower):
/**

 *    Creator :  bizarre

 *    Date : 4/2020

 *    Problem Number : 428

**/

#include <bits/stdc++.h>

using namespace std;

#define bizarre     ios_base::sync_with_stdio( false ), cin.tie( nullptr ), cout.tie( nullptr );

/*

vector<int> prime_Numbers;

int primes[1000005];

void prime(int n)

{

    primes[1]=primes[0]=0;

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

        primes[i]=true;

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

    {

        if(primes[i]==true)

        {

            prime_Numbers.push_back(i);

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

                primes[j]=false;

        }

    }

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

        primes[i]+=primes[i-1];

}



void move1step(long long& a, long long& b, long long q)

{

    long long c= a-q*b;

    a=b;

    b=c;

}

long long egcd(long long r0,long long r1,long long& x0,long long& y0)

{

    long long x1=0,y1=1;

    x0=1;

    y0=0;

    while(r1)

    {

        long long q=r0/r1;

        move1step(r0,r1,q);

        move1step(x0,x1,q);

        move1step(y0,y1,q);

    }

    return r0;

}

bool  solve (long long a, long long b, long long c,long long&x, long long& y)

{

    long long g=egcd(a,b,x,y);

    int m=c/g;

    x*=m;

    y*=m;

    return c%g==0;

}







int npr(long long n, long long r)

{

    return (fact[n] * inv[n - r]) % mod;

}



int gcd(int a, int b)

{

    if(b==0)

        return a;

    return gcd(b,a%b);

}

void PrimeFactorization(int n)

{

    for(int i=2; i*i<=n; i++)

    {

        int c=0;

        if(n%i==0)

        {

            while(n%i==0)

            {

                n/=i;

            }



        }

    }



}

const int inf = 2e9, mod = 1e9 + 7,N=1e5+5;

long long fact[N], inv[N];

long long fp(long long base, long long exp)

{

    if (exp == 0)

        return 1;

    long long ans = fp(base, exp / 2);

    ans = (ans * ans) % mod;

    if (exp % 2 != 0)

        ans = (ans * (base % mod)) % mod;

    return ans;

}

void pre(long long n)

{

    fact[0] = 1;

    inv[0] = 1;

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

    {

        fact[i] = (i * fact[i - 1]) % mod;

        inv[i] = fp(fact[i], mod - 2);

    }

}

int ncr(long long n, long long r)

{

    return (((fact[n] * inv[n - r]) % mod) * inv[r]) % mod;

}

long long ncrtane(long long n, long long r)

{

    long long ans=1,cntr=1;

    for(int i=n;i>n-r;i--)

    {

        ans*=i;

        ans/=cntr;

        cntr++;

    }

    return ans;

}

*/

const int N=1e5+5;

vector<int>graph[N];

bool vis[N];

int dp[N];

void dfs(int node)

{

    vis[node]=true;

    for(auto child:graph[node])

    {

        if(!vis[child])dfs(child);

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

    }

}

int main()

{

    bizarre

    int n,m,mxm=0;

    cin>>n>>m;

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

    {

       int x,y; cin>>x>>y;

       graph[x].push_back(y);

    }

    memset(vis,0,sizeof vis);

    memset(dp,0,sizeof dp);

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

    {

        dfs(i);

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

    }

    cout << mxm;

    return 0;

}




// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

#define pb push_back

#define POP pop_back()

#define ll long long

#define db double

#define POP pop_back()

#define endl '\n'

const int maxn = 1e5 + 5;

const int inf = 0x3f3f3f3f;

const int mod = 1e9 + 7;

struct edg{

	int to , nxt;

};

struct node{

	int e , s;

};

edg e[maxn];

int head[maxn] , in[maxn] , s[maxn];

int main(){

	int n , m;cin >> n >> m;

	int u , v;

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

		scanf("%d%d",&u,&v);

		e[i].nxt = head[u];

		e[i].to = v;

		head[u] = i;

		in[v]++;

	}

	queue<node>q;

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

		if(in[i] == 0)q.push((node){i,0});

	}

	node now;

	while(!q.empty()){

		now = q.front() , q.pop();

		for(int i = head[now.e];i != 0;i = e[i].nxt){

			in[e[i].to]--;

			s[e[i].to] = max(s[e[i].to] , now.s+1);

			if(in[e[i].to] == 0)q.push((node){e[i].to , s[e[i].to]});

		}

	}

	int ans = 0;

	for(int i = 1;i <= n;i++)ans = max(ans , s[i]);

	cout << ans << endl;

	return 0;

}

// Fastest known solution for this problem: