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>
using namespace std; 
int a[100009];
class Graph 
{ 
    int V,p=0,g;   
    list<int> *adj; 
    void topologicalSortUtil(int v, bool visited[], stack<int> &Stack); 
public: 
    int x[100009];
    long long t,ans;
    Graph(int V);   
    void addEdge(int v, int w); 
    int  topologicalSort(); 
}; 


Graph::Graph(int V) 
{ 
    this->V = V; 
    adj = new list<int>[V+1]; 
} 
void Graph::addEdge(int v, int w) 
{ 
    adj[v].push_back(w); 
} 


void Graph::topologicalSortUtil(int v, bool visited[],  
                                stack<int> &Stack) 
{ 
    visited[v] = true; 
    list<int>::iterator i; 
    for (i = adj[v].begin(); i != adj[v].end(); ++i) 
        if (!visited[*i]) 
            topologicalSortUtil(*i, visited, Stack); 
    Stack.push(v); 
} 


int  Graph::topologicalSort() 
{ 
    stack<int> Stack; 
    bool *visited = new bool[V]; 
    for (int i = 1; i <= V; i++) 
        visited[i] = false; 
    for (int i = 1; i <= V; i++) 
      if (visited[i] == false) 
        topologicalSortUtil(i, visited, Stack);
        int i=1; 
    while (Stack.empty() == false) 
    { 
        a[i]=Stack.top() ;
        Stack.pop(); 
        i++;
    } 
for(int i=0;i<100009;i++)
x[i]=0;
g=V-1;
    for(int i=1;i<V;i++)
    {
        t=a[g];
        for(auto z : adj[t])
        {
            p=1+x[z];
            x[a[g]]=max(x[a[g]],p);
        }
        g--;
    }
    cout<<*max_element(x,x+100009);
    return 0;
}

int main() 
{ 
    long long n,m,x,y,t,ans;
    cin>>n>>m;
    Graph g(n);  
    for(int i=1;i<=m;i++)
    {
        cin>>x>>y;
        g.addEdge(x, y);
    }
    g.topologicalSort();
    return 0; 
}