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: Time Limit Exceeded)

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<list>
#include<cstdint>
#include<bits/stdc++.h>
#include<queue>
#include<set>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep_r(i, n) for(int i = n-1; i >= 0; i--)
#define rep1(i, n) for(int i = 1; i <= (int)(n); i++)
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((ll)(x).size())
#define bit(n) (1LL<<(n))
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

#define INF bit(60)
#define pb push_back
#define mod 1000000007

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

using namespace std;
using uif=uint_fast64_t;
using ll=long long int;

int qp(int a,ll b){int ans=1;do{if(b&1)ans=1ll*ans*a%mod;a=1ll*a*a%mod;}while(b>>=1);return ans;}
int qp(int a,ll b,int mo){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}

int gcd(int a,int b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int main(void){

  ll n,m;
  cin>>n>>m;
  vector<ll> x(m);
  vector<ll> y(m);
  vector<ll> dp(n+1,0);
  vector<ll> old_dp(n+1,0);
  vector<pair<ll,ll>> c1;
  vector<pair<ll,ll>> c2;
  ll cnt=0;

  rep(i,m){
    cin>>x[i];
    cin>>y[i];
    c1.pb(make_pair(x[i],y[i]));
  }

  while(c1.size()>0){
    for(auto p:c1){
      old_dp[p.second] = max(dp[p.second]+1,dp[p.first]);
    }
    rep(i,n+1){
      dp[i]=old_dp[i];
    }
    cnt++;
    c2.clear();
    for(auto p:c1){
      if(dp[p.first] >= cnt){
        c2.pb(p);
      }
    }
    c1.clear();
    swap(c1,c2);
    //for(auto c:c1){
      //cout << c.first << "," << c.second << endl;
    //}
    //cout << endl;
    //for(auto d:dp){
      //cout << d << " ";
    //}
    //cout << endl;
    //if(cnt>2) break;

  }

  /*
  rep(i,m){
    ll upd=-1;

    rep(j,m){
      if(dp[ x[j] ] + 1 > dp[ y[j] ]){
        dp[ y[j] ] = dp[ x[j] ] + 1;
        upd=0;
      }
    }
    if( upd==-1)  break;

  }
  */

  cout << *max_element(all(dp)) << endl;

  return 0;

}