eolymp
bolt
Try our new interface for solving problems
Problems

Highways and King

Highways and King

There are n towns conveniently labeled with 1, 2, ..., n in Flatland Kingdom and m bidirectional highways are allowed to be built.

The i-th highway can be built between cities ai and bi with cost ci.

The Flatland Road Company plans to build the (n - 1) allowed highways with the least total cost to connect any of two cities directly or indirectly.

The King of Flatland is going to remove some of the highways from the list of the allowed highways. He would like to know the minimum number of highways to be removed to strictly increase the total cost.

Note that the total cost is considered as +∞ if no valid (n - 1) highways exist after removing. It is also counted as <<total cost strictly increases>>.

Input

Contains zero or more test cases and is terminated by end-of-file. For each test case:

The first line contains two integers n (2n50) and m (n - 1mn2). The i-th of the following m lines contains ai, bi, ci (1ai, bin, 1ci109).

  • Any two cities will be connected if all m allowed highways are built.
  • The sum of n does not exceed 103.

Output

For each case, output an integer which denotes the result.

Time limit 1 second
Memory limit 128 MiB
Input example #1
3 3
1 2 1
1 3 2
2 3 3
3 4
1 2 1
1 2 1
1 3 2
1 3 3
3 4
1 2 1
1 2 1
1 3 2
1 3 2
4 6
1 2 1
1 3 1
1 4 1
2 3 1
2 4 1
3 4 1
Output example #1
1
1
2
3
Source 2022 Azerbaijan ICPC Qualification