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: Runtime Error)

#include<bits/stdc++.h>
#include<stdio.h>
#include<stdlib.h>

using namespace std;
#define endl "\n"
#define FILEIO freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define LL long long 
#define mod 1000000007LL
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define pp pop_back
//#define p push
#define I insert
#define bg begin()
#define ed end()
#define sz size()
#define all(v) v.begin(),v.end()
typedef vector<LL> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef map<LL ,LL > mii;
//typedef priority_queue<int> pq;
typedef queue<int>que;
typedef stack<int>stk;
#define Inf 100000005LL
#define f first
#define s second





vi vect_2pow;
void pow_of_2(){
    vect_2pow.assign(32,0);
      for(int i=0;i<=32;i++){
           LL y = 1<<i;
           vect_2pow[i]=y;
      }
}
bool Ispalindrome(string str){
        LL len = str.size();
		bool flag;
		for(int i=0;i<len/2;i++){
				if(str[i]==str[len-i-1])
				     {
						 flag=0;
						 continue;
					 }
					 else 
					    {
							flag=1;
							break;
						}
			}
		if (flag==1)
		  return false;
		else return true;  
		
}
vector<LL> prime_check;
mii get_high_prime;
int high_prime_factor(){
	//cout<<"ecd";
	LL max_prime=0;
	for(int i=2;i<100000;i++)
	{
		LL n=i;
		
		while(n%2==0)
		{
			max_prime=2;
			n>>=1;
		}
		for(int j=3;j<=sqrt(n);j++)
		{
			while(n%j==0){
				max_prime=j;
				n/=j;
			}
		}
		if(n>2)
		  max_prime=n;
		  
		//prime_check.pb(max_prime);
		get_high_prime[i]=max_prime;
	}
	
			
}

//const LL inf = 10e+9;
void max_self(LL &a,LL b){
	a=max(a,b);

}
const int maxi= 1e5+5;
vector<int> vect[100000];
vector<int> dp(maxi);
vi indegre(maxi);
LL j=0;

vector<bool > visited;
void dfs(int k)
{
	//assert(!visited[k]);
	visited[k]=true;
	for(auto x: vect[k])
	{
        dp[x]=max(dp[x],dp[k]+1);
		--indegre[x];
		if(indegre[x]==0)
		    dfs(x);
	}
}
int main()
{
	 
	LL n , e;
	cin>>n>>e;
    visited.assign(n+1,false);
	for(int i=0;i<e;i++)
	{
		LL a,b;
		cin>>a>>b;
		vect[a].pb(b);
		++indegre[b];
		
	}
	for(int i=1;i<=n;i++)
	{
		if(!visited[i] && indegre[i]==0)
		    dfs(i);
		
	}
	int ans=0;
	for(int i=1;i<=n;i++)
	   ans=max(ans,dp[i]);
	 cout<<ans<<endl;  
}