Under construction

Still being built. Things may change

Ant mansion collapse

Time limit2sMemory limit1024 MB

Summary
Find the minimum total risk of paths linking nests 1 to N to nest 0, then the least stamina among those, or -1 if some nest is unreachable.
Level

Medium6 of 10

Topics
Minimum spanning tree, Graph, Tree
Solved
No attempts yet

Problem

An ant kingdom has N+1N+1 ant nests, numbered from 00 to NN. There are MM bidirectional paths, each connecting two different nests. After a heavy rain, the kingdom is at risk of collapse, so the collapse risk of every path was measured. The collapse risk of path ii is did_{i}. By default, every path has a different risk. The exception is that paths directly connected to nest 00 may have equal risks.

So, are the nests safe?

At the moment, NN ants numbered from 11 to NN are gathered at nest 00. Ant ii sets out on a trip to check whether nest ii is safe. The ants want to minimize the total risk of the paths they use. In other words, if the set of path numbers used by the NN ants is S={s1,s2,⋯ ,sM}S=\{s_{1}, s_{2}, \cdots, s_{M}\}, they want to minimize the following value WW.

W=∑i=1MdsiW=\sum_{i=1}^{M}{d_{s_{i}}}

Each time an ant uses a path, it spends 11 unit of stamina. Among the cases where WW is minimal, help the ants minimize the total stamina they spend.

Input

The first line contains NN and MM, separated by a space. (1≤N≤200 000;1≤M≤200 000)(1\leq N \leq 200\,000; 1 \leq M \leq 200\,000)

Each of the next MM lines contains uu, vv, and dd, separated by spaces. This means the path connecting nest uu and nest vv has risk dd. (0≤u,v≤N;u≠v;1≤d≤109)(0\leq u,v \leq N; u \neq v; 1\leq d \leq 10^{9})

Output

When WW is minimal, print the minimum total stamina the ants spend. If it is impossible to check all NN nests, print −1-1.

Examples2

  1. Example 1

    Input
    3 4
    0 1 3
    0 2 3
    1 3 10
    2 3 5
    
    Expected output
    4
    
  2. Example 2

    Input
    2 1
    0 1 3
    
    Expected output
    -1