Problem:
Score : 100 points

Problem Statement

There is a directed graph G with N vertices and M edges.
The vertices are numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed edge goes from Vertex x_i to y_i.
G does not contain directed cycles.

Find the length of the longest directed path in G.
Here, the length of a directed path is the number of edges in it.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq x_i, y_i \leq N

All pairs (x_i, y_i) are distinct.

G does not contain directed cycles.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the length of the longest directed path in G.

Sample Input 1

4 5
1 2
1 3
3 2
2 4
3 4

Sample Output 1

3

The red directed path in the following figure is the longest:

Sample Input 2

6 3
2 3
4 5
5 6

Sample Output 2

2

The red directed path in the following figure is the longest:

Sample Input 3

5 8
5 3
2 3
2 4
5 2
5 1
1 4
4 3
1 3

Sample Output 3

3

The red directed path in the following figure is one of the longest:

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast", "unroll-loops")
using namespace std;
#define ll long long
//#define int ll
#define FOR(i,a,b) for (int i = (a); i<(b); i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)
#define RREP(i,n) for (int i=(n)-1; i>=0; i--)
#define f first
#define s second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define SQ(x) (x)*(x)
#define pii pair<int, int>
#define endl '\n'
//#define TOAD
#ifdef TOAD
#define bug(x) cerr<<__LINE__<<": "<<#x<<" is "<<x<<endl
#define IOS() 
#else
#define bug(...)
#define IOS() ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#endif
const ll inf = 1ll<<60;
const int iinf=2147483647;
const ll mod = 998244353;
const ll maxn=1e3+5;
ll pw(ll x, ll p){
    ll ret=1;
    while (p>0){
        if (p&1){
            ret*=x;
            ret%=mod;
        }
        x*=x;
        x%=mod;
        p>>=1;
    }
    return ret;
}
ll inv(ll x){
    return pw(x, mod-2);
}
bool cmp(pii x, pii y) {
    return x.s<y.s;
}
signed main(){
    IOS();
    int n,m; cin>>n>>m;
    vector<int> graph[n+1], in(n+1), pointing[n+1];
    REP(i,m){
        int x,y; cin>>x>>y;
        graph[x].pb(y);
        pointing[y].pb(x);
        in[y]++;
    }
    vector<int> res;
    queue<int> qu;
    REP1(i,n){
        //cout<<in[i]<<' ';
        if (in[i]==0) qu.push(i);
    }
    //cout<<endl;
    while(qu.size()){
        int j=qu.front();
        res.pb(j);
        REP(i,graph[j].size()){
            in[graph[j][i]]--;
            if (in[graph[j][i]]==0){
                qu.push(graph[j][i]);
            }
        }
        qu.pop();
    }
    vector<int> dp(n+1);
    int ans=0;
    //cout<<res.size()<<endl;
    REP(i,n){
    	bug(i);
        int k=res[i];
        REP(j,pointing[k].size()){
        	//cout<<pointing[k][j]<<' ';
            dp[k]=max(dp[k], dp[pointing[k][j]]+1);
        }
        ans=max(ans, dp[k]);
        //cout<<endl;
        //cout<<ans<<endl;
    }
 
    cout<<ans<<endl;
 
 
}