Problem: Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).

Constraints

* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 100,000
* 0 ≤ wi ≤ 10,000
* The graph is connected
* There are no parallel edges
* There are no self-loops

Input


|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1


, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.

si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.

Output

Print the sum of the weights of the Minimum Spanning Tree.

Examples

Input

4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5


Output

3


Input

6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6


Output

5

Solution (in JAVA):

Solution:
import java.util.Arrays;
import java.util.Scanner;

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_2_A&lang=jp
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int V = sc.nextInt();
        int E = sc.nextInt();
        Edge[] edges = new Edge[E];
        for (int i = 0; i < E; i++) {
            int s = sc.nextInt();
            int t = sc.nextInt();
            int w = sc.nextInt();
            edges[i] = new Edge(s, t, w);
        }
        Arrays.sort(edges);
        UnionFind uf = new UnionFind(V);
        int weight = 0;
        for (Edge e : edges) {
            if (!uf.equals(e.s, e.t)) {
                weight += e.w;
                uf.merge(e.s, e.t);
                if (uf.groups == 1) {
                    break;
                }
            }
        }
        System.out.println(weight);
    }

    static class UnionFind {
        public int[] v;
        int groups;

        UnionFind(int n) {
            v = new int[n];
            for (int i = 0; i < n; i++) {
                v[i] = i;
            }
            groups = n;
        }

        int find(int p) {
            if (v[p] == p) {
                return p;
            }
            return v[p] = find(v[p]);
        }

        boolean equals(int a, int b) {
            return this.find(a) == this.find(b);
        }

        int merge(int p, int q) {
            p = find(p);
            q = find(q);
            if (p != q) {
                v[q] = p;
                groups--;
            }
            return groups;
        }
    }

    public static class Edge implements Comparable<Edge> {
        public int s, t, w;

        Edge(int s, int t, int w) {
            this.s = s;
            this.t = t;
            this.w = w;
        }

        @Override
        public int compareTo(Edge o) {
            return this.w - o.w;
            // return o.w - this.w;
        }
    }
}