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 Java (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

/******************************************************************************

                            Online Java Compiler.
                Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/

import java.io.*;
import java.util.*;
public class Main
{
    
    
    
    public static int func(ArrayList <Integer>ls[], int vis[], int len, int j,int dp[])
    {
        int temp=0;
        int ans=0;
        
        if(ls[j]==null)
        {
            return 0;
        }
         
        if( vis[j]==1)
        {
            return dp[j];
        }
       
        
        else {
            for(int k =0;k<ls[j].size();k++)
            {
            
            
            temp= 1+ func(ls,vis,len,ls[j].get(k),dp);
            
           // dp[ls[j].get(k)]=temp;
            ans=Math.max(temp,ans);
            
            }
        }
        vis[j]=1;
        return dp[j]=ans;
        
        
    }
    
    
	public static void main(String[] args) throws Exception {
	//	System.out.println("Hello World");
		
		int N,M;
		BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
	String s=br.readLine();
	String ss[]=s.split(" ");
	N=Integer.parseInt(ss[0]);
	M=Integer.parseInt(ss[1]);
	int x=0,y=0;
	int ans=0;
	int vis[]=new int[N+1];
	int dp[]=new int [N+1];
	Arrays.fill(dp,0);
	//int dp[][]=new int[N+1][N+1];
	ArrayList<Integer>ls[]=new ArrayList[N+1];
	for(int i=0;i<M;i++)
	{
	    s=br.readLine();
	    ss=s.split(" ");
	 x=Integer.parseInt(ss[0]);
	y=Integer.parseInt(ss[1]);
	 if(ls[x]==null)
	 {
	     ls[x]=new ArrayList();
	 }
	    ls[x].add(y);
	    
	}
	for(int i=1;i<=N;i++)
	{
	 if(vis[i]==1)
	 {
	     ans=Math.max(ans,dp[i]);
	     continue;
	 }
	 if(ls[i]!=null)
	 {
	     vis[i]=1;
	     for(int j=0;j<ls[i].size();j++)
	     {
	      int temp =1+func(ls,vis,0,ls[i].get(j),dp);
	      
	      ans=Math.max(temp,ans);
	     }
	 }
	   dp[i]=ans; 
	}
	System.out.println(ans);
   
	}
}