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 <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
#include <climits>
using namespace std; 
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define vii             vector<pii> 
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define f(i,x,n)        for(int i=x;i<n;i++)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count()); 
 
 unordered_map<int,list<int> > m ;
 unordered_set<int> s ; 
 unordered_map<int,int> dist ; 
 int ans=INT_MIN; 
 //int calculated[100000] ; 
 void dfs(int src,int fth){ 
       if(fth !=-1 && (dist[src] >= dist[fth] + 1) ){
          ans=max(ans,dist[src]) ; 
           return ;
        }
       if(fth==-1) dist[src] = 0 ;
       else
       dist[src] = max(dist[src] , dist[fth] + 1 ) ;
       //calculated[src] = dist[src] ;
       ans=max(ans,dist[src]) ; 
       //cout<<src<<" "<<dist[src]<<"\n";
       for(auto x:m[src] ) {
       	    dfs(x,src) ; 
       }
       return ;                         
 
 }
int32_t main(){
 
      
	   ios::sync_with_stdio(0);
       cin.tie(0);
 
      int n ;
      cin>>n ; 
      int v[n+1]={0}; 
       f(i,0,n) { 
         dist[i+1] = 0 ;
        // calculated[i+1] = -1 ; 
    } 
      int src = 0 ;
      vi s_point ; 
      w(edge){
 
      	 int a,b ;
      	 cin>>a>>b ; 
      	 m[a].pb(b) ;  
         v[b] = true ;
     }
 
     for(int i=1;i<=n;i++){
     	 if(!v[i]) {
     	 	dfs(i,-1) ;
     	 }
     }
     
     cout<<ans ;
}