Skip to main content
LIVE CONTEST: ALGOR Contest: Round #4 (Special Challenge) is live now!

~/algor $ cat problems/minimal-qamrovchi-daraxt

Minimum spanning tree

Medium 1000 ms 128 MB 66%
Share

A graph with $n$ vertices and $m$ undirected edges is given; each edge has a weight $w$. The vertices are numbered from $1$ to $n$. The minimum spanning tree of the graph is a set of edges connecting all vertices with the smallest total weight. Find this total. If the graph is not connected, print $-1$.

A weighted graph with its minimum spanning tree highlighted

Input

The first line contains two integers: $n$ and $m$. Each of the next $m$ lines contains three integers: $u$, $v$ and $w$ — the vertices joined by the edge and the edge weight.

Output

One integer — the weight of the minimum spanning tree or $-1$.

Note

$1 \le n \le 10^{5}$
$0 \le m \le 2 * 10^{5}$
$1 \le u, v \le n$
$-10^{9} \le w \le 10^{9}$

Examples

Input #1
4 5
1 2 1
1 3 4
2 3 2
2 4 5
3 4 3
Output #1
6
Input #2
3 1
1 2 5
Output #2
-1

Sign in to submit a solution

Signing up is free and takes a minute.