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 <bits/stdc++.h>
using namespace std;
// #define int long long int
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define gcd __gcd
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define gcd __gcd
//SUNEET KUMAR -->  ਸੁਨੀਤ  
int power(int a,int n)//binary expo
{
    int r;
    if(n==0)
        return 1;
    else if(n==1)
        return a;
    else
        r=power(a,n/2);
    if(n%2==0)
        return ((r)%mod)*((r)%mod)%mod;
    else
        return ((((r)%mod)*((r)%mod))%mod*((a)%mod))%mod;
}
int max(int a,int b)
{
    if(a>b)
        return a;
    else
        return b;
}
int maxot(int a,int b,int c)
{
    if(a>=b&&a>=c)
    {
        return a;
    }
    if(b>=a&&b>=c)
    {
        return b;
    }
    if(c>=b&&c>=a)
    {
        return c;
    }  
}
vector<int>v[100005];
int dist[100005],visit[100005];
signed main()
{ 
    // fastio;
    int n,m;
    // cin>>n>>m;
    scanf("%d%d",&n,&m);
    int x,y;
    for(int i=0;i<m;i++)
    {
        // cin>>x>>y;
        scanf("%d%d",&x,&y);
        v[x].pb(y);
    }
    queue<int>q;
    int p;
    for(int i=1;i<=n;i++)
    {
        if(visit[i]==0)
        {
            visit[i]=1;
            q.push(i);
             while(!q.empty())
            {
            p=q.front();
            q.pop();
            for(int j=0;j<v[p].size();j++)
            {
                if(dist[v[p][j]]<dist[p]+1)
                {
                    q.push(v[p][j]);
                    visit[v[p][j]]=1;
                    dist[v[p][j]]=dist[p]+1;
                }  
            }
            }
        }
    }
    vector<int>v;
    for(int i=1;i<=n;i++)
    {
        // cout<<dist[i]<<endl;
        v.pb(dist[i]);
    }
    sort(v.begin(),v.end());
    cout<<v[v.size()-1]<<endl;

}