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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(non_snake_case)]

use std::cmp;

#[allow(dead_code)]
pub struct Scanner<'a> {
    _buf: &'a str,
    iter: ::std::str::SplitWhitespace<'a>,
}

#[allow(dead_code)]
impl<'a> Scanner<'a> {
    pub fn new(buf: &'a str) -> Self {
        Scanner {
            _buf: buf,
            iter: buf.split_whitespace(),
        }
    }

    pub fn array<T>(&mut self, n: usize) -> Vec<T>
        where T: ::std::str::FromStr,
              T::Err: ::std::fmt::Debug,
    {
        let mut res = Vec::with_capacity(n);
        for _ in 0..n {
            res.push(self.value());
        }
        res
    }

    pub fn value<T>(&mut self) -> T
        where T: ::std::str::FromStr,
              T::Err: ::std::fmt::Debug,
    {
        self.next().parse().unwrap()
    }

    pub fn chars(&mut self) -> Vec<char> {
        self.next().chars().collect()
    }

    fn next(&mut self) -> &str {
        self.iter.next().unwrap()
    }
}

#[allow(dead_code)]
const INF: i64 = 1_010_000_000_000_000_000;

struct Solver {
    N: i64,
    G: Vec<Vec<i64>>,
    memo: Vec<Option<i64>>,
}

impl Solver {
    fn new(N: i64, G: Vec<Vec<i64>>) -> Self {
        Solver {
            N: N,
            G: G,
            memo: vec![None; N as usize],
        }
    }

    // 頂点 i から始まる有向パスの最大長
    fn f(&mut self, i: i64) -> i64 {
        if self.memo[i as usize].is_none() {
            let mut res = 0;
            for j in 0..self.G[i as usize].len() {
                let to = self.G[i as usize][j as usize];
                res = cmp::max(res, 1+self.f(to));
            }
            self.memo[i as usize] = Some(res);
        }
        self.memo[i as usize].unwrap()
    }

    fn solve(&mut self) -> i64 {
        let mut res = 0;
        for i in 0..self.N {
            res = cmp::max(res, self.f(i));
        }
        res
    }
}

fn main() {
    let mut _input = String::new();
    {
        use ::std::io::Read;
        ::std::io::stdin().read_to_string(&mut _input).unwrap();
    }
    let mut sc = Scanner::new(&_input);

    let N = sc.value::<i64>();
    let M = sc.value::<i64>();
    let mut G = vec![vec![]; N as usize];
    for _ in 0..M {
        let x = sc.value::<i64>() - 1;  // 0-based
        let y = sc.value::<i64>() - 1;  // 0-based
        G[x as usize].push(y);
    }

    let mut sol = Solver::new(N, G);
    let ans = sol.solve();

    println!("{}", ans);
}