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: Wrong Answer)

#include <iostream>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <cstring>
#include <string.h>
#include <sstream>
#include <iomanip>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <tuple>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
 
typedef unsigned long ul;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
const ll MOD = 1e9+7;
const ll CFMOD = 998244353;
const double PI  = 3.14159265358;
const double EPS = 1e-8;
 
#define INT_MX 2147483647
#define INF (((int)1e9) + 7)
#define LLINF ((ll)1e18)
#define F first
#define S second
#define MP(x,y) make_pair(x,y)
#define sz(v) ((int)v.size())
#define rs(n) resize(n)
#define ALL(v) v.begin(),v.end()
#define reset(v) memset((v),0,sizeof(v))
#define EB emplace_back
#define PB push_back
#define PF push_front
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define REP(i,n,m) for(int i=(n);i<(m);i++)
#define vec vector
#define __ <<' '<<
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

void solve_prob(){
    int n, m, x, y, t;
    srand((unsigned int)time(NULL));
    
    cin >> n >> x >> m >> y;
    
    long double a, b;
    int px, py;
    a = b = 0;
    t = 1000000;
    rep(i,t){
        px = py = 0;
        rep(j,n) px += rand()%x + 1;
        rep(j,m) py += rand()%y + 1;
        
        if(px > py) a++;
        if(px < py) b++;
    }
    cout << a / t << '\n' << b / t << '\n';
}

vec<int> dp;
vec<vec<int>> v;

int dfs(int x){
    if(dp[x]) return dp[x];
    
    int res = 1;
    for(auto i:v[x])
        res = max(res, dfs(i));
    dp[x] = res;
    return res + 1;
}

void solve(){
    int n, m;
    vec<int> out;
    pii ipt;
    
    cin >> n >> m;
    dp.rs(n);
    v.rs(n);
    out.rs(n);
    rep(i,m){
        cin >> ipt.F >> ipt.S;
        out[ipt.F-1]++;
        v[ipt.S-1].EB(ipt.F-1);
    }
    rep(i,n){
        if(out[i] == 0)
            dfs(i);
    }
    
    int ans = 0;
    rep(i,n)
        ans = max(dp[i], ans);
    cout << ans << '\n';
    
}

 
int main(){
    int t = 1;
    
    while(t--){
        solve();
    }
}