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>
#include <numeric>
#define M 1000000007
#define pb push_back
#define mp make_pair
#define forz(i,s,n) for(lli i=s;i<n;i++)
#define fore(i,n,s) for(lli i=n;i>=s;i--)

#define fast_io                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);

typedef long long int lli;
//typedef pair<long long int, long long int> pll;
typedef unsigned long long int ull;
using namespace std;

lli recpow(lli x, lli n)
{
    if (n == 0)
        return 1;
    else if (n % 2 == 0) //n is even
        return recpow((x * x) % M, n / 2);
    else //n is odd
        return (x * recpow((x * x) % M, (n - 1) / 2)) % M;
}

bool sortbysec(const pair<int, int> &a,
               const pair<int, int> &b)
{
    return (a.second < b.second);
}

bool comp(pair<lli, lli> a, pair<lli, lli> b)
{
    if(a.first==b.first)
        return a.second<b.second;
    else
        return a.first<b.first;
}

vector<lli> dp;
vector<vector<lli>> a,b;
void bfs(lli x, lli n)
{
    queue<lli> q;
    //lli vis[n]={0};
    q.push(x);
    while(!q.empty())
    {
        lli t=q.front();
        q.pop();
        // if(vis[t]==1)
        // continue;
        // vis[t]=1;
        forz(i,0,a[t].size())
        {
            // if(vis[a[t][i]]==1)
            // continue;
            q.push(a[t][i]);
            dp[a[t][i]] = max(dp[a[t][i]], dp[t]+1);
        }
    }
}

//code begins here
int main()
{
    fast_io;
    lli n,m;
    cin>>n>>m;
    vector<lli> u;
    forz(i,0,n)
    {
        a.pb(u);
        b.pb(u);
    }
    forz(i,0,m)
    {
        lli x,y;
        cin>>x>>y;
        x--, y--;
        a[x].pb(y);
        b[y].pb(x);
    }
    forz(i,0,n)
    dp.pb(0);
    //vector<lli> v;
    forz(i,0,n)
    {
        if(b[i].size()==0)
        bfs(i, n);
    }
    lli ans=0;
    forz(i,0,n)
    ans=max(ans, dp[i]);
    cout<<ans<<endl;
    return 0;
}