Skip to content
UPEEC

Cheapest Power Grid

Hard#graphs#minimum-spanning-tree#union-find· solved by 0 · time limit 2.0 s

Connect nn villages to one power grid. Building line u v costs ww. Choose lines so every village is connected at minimum total cost (a minimum spanning tree — try Kruskal's algorithm). Print IMPOSSIBLE if they can't all be connected.

Input

nn and mm, then mm lines u v w.

Output

The minimum total cost or IMPOSSIBLE.

Constraints

1≤n≤1051 \le n \le 10^5, 0≤m≤2×1050 \le m \le 2 \times 10^5

Sample 1

Input

4 5
1 2 1
2 3 4
1 3 3
3 4 2
2 4 5

Output

6

Sample 2

Input

3 1
1 2 5

Output

IMPOSSIBLE

Sign in or join free to run and submit your solution.