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.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int v = Integer.parseInt(scan.next());
        int e = Integer.parseInt(scan.next());
        List<Edge> edgeList = new ArrayList<Edge>(e);
        for (int i = 0; i < e; i++) {
            int s = Integer.parseInt(scan.next());
            int t = Integer.parseInt(scan.next());
            int w = Integer.parseInt(scan.next());
            edgeList.add(new Edge(s, t, w));
        }
        scan.close();

        Collections.sort(edgeList, new Comparator<Edge>() {
            @Override
            public int compare(Edge o1, Edge o2) {
                if (o1.w != o2.w) {
                    return o1.w - o2.w;
                }
                if (o1.s != o2.s) {
                    return o1.s - o2.s;
                }
                return o1.t - o2.t;
            }
        });

        UnionFind uf = new UnionFind(v);
        long ans = 0;
        for (Edge edge : edgeList) {
            if (!uf.isSameParent(edge.s, edge.t)) {
                uf.union(edge.s, edge.t);
                ans += edge.w;
            }
        }

        System.out.println(ans);
    }

    static class Edge {
        int s;
        int t;
        int w;

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

    }

    static class UnionFind {
        int[] elements;

        public UnionFind(int elementsCount) {
            elements = new int[elementsCount];
            for (int i = 0; i < elementsCount; i++) {
                elements[i] = i;
            }
        }

        public void union(int x, int y) {
            elements[getParent(y)] = getParent(x);
        }

        public boolean isSameParent(int x, int y) {
            return getParent(x) == getParent(y);
        }

        public int getParent(int x) {
            int parent = elements[x];
            if (x != parent) {
                elements[x] = getParent(parent);
            }
            return elements[x];
        }
    }

}