Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.

You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.

Help Pashmak, print the number of edges in the required path.


-----Input-----

The first line contains two integers n, m (2 ≤ n ≤ 3·10^5; 1 ≤ m ≤ min(n·(n - 1), 3·10^5)). Then, m lines follows. The i-th line contains three space separated integers: u_{i}, v_{i}, w_{i} (1 ≤ u_{i}, v_{i} ≤ n; 1 ≤ w_{i} ≤ 10^5) which indicates that there's a directed edge with weight w_{i} from vertex u_{i} to vertex v_{i}.

It's guaranteed that the graph doesn't contain self-loops and multiple edges.


-----Output-----

Print a single integer — the answer to the problem.


-----Examples-----
Input
3 3
1 2 1
2 3 1
3 1 1

Output
1

Input
3 3
1 2 1
2 3 2
3 1 3

Output
3

Input
6 7
1 2 1
3 2 5
2 4 2
2 5 2
2 6 9
5 4 3
4 3 4

Output
6



-----Note-----

In the first sample the maximum trail can be any of this trails: $1 \rightarrow 2,2 \rightarrow 3,3 \rightarrow 1$.

In the second sample the maximum trail is $1 \rightarrow 2 \rightarrow 3 \rightarrow 1$.

In the third sample the maximum trail is $1 \rightarrow 2 \rightarrow 5 \rightarrow 4 \rightarrow 3 \rightarrow 2 \rightarrow 6$.

Source: codeforces | Difficulty: HARD
Tags: Dynamic programming

Sorting


Solutions:
from sys import *
f = list(map(int, stdin.read().split()))
(n, m) = (f[0], f[1])
d = [[] for i in range(100001)]
for j in range(2, len(f), 3):
	(x, y, w) = f[j:j + 3]
	d[w].append((y, x))
s = [0] * (n + 1)
for q in d:
	for (y, k) in [(y, s[x]) for (y, x) in q]:
		s[y] = max(s[y], k + 1)
print(max(s))


from sys import *
a = list(map(int, stdin.read().split()))
n = a[0]
m = a[1]
inf = 10 ** 5 + 1
g = [[] for i in range(inf)]
for i in range(2, len(a), 3):
	(b, c, w) = a[i:i + 3]
	g[w].append((c, b))
n += 1
s = [0] * n
for l in g:
	for (i, k) in [(i, s[j]) for (i, j) in l]:
		s[i] = max(s[i], k + 1)
print(max(s))