Lumber Jack and Cycles

Let G = (V, E) be a weighted oriented multigraph. A vertex T ∈ V is called special if there exists at least one negative cycle that is accessible from T. Please compute the number of special vertices of G.

Input

The first line of input contains |V| and |E|.
Each of the following |E| lines contains a triple (x, y, w), denoting an edge x → y of weight w.

Output

A single integer — the number of special vertices of G.

Constraints

  • 1 ≤ |V| ≤ 100 000
  • 1 ≤ |E| ≤ 250 000
  • 1 ≤ x, y ≤ |V|
  • -104 ≤ w ≤ 104

Sample

InputOutputExplanation
6 6
1 2 5
1 3 3
3 6 -1
5 5 -1
2 4 -2
4 2 1
4

There are two negative cycles:

  • 2⇄4, which is accessible from vertices 1, 2, 4.
  • 5↺, which is accessible from vertex 5.

    

Questions?

Sponsors Gold