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.*;

public class Main {

    private static int MOD = 1_000_000_007;

    public static void main(String[] args) {
        final Scanner scanner = new Scanner(System.in);
        solve(scanner);
    }

    static void solve(Scanner scanner) {
        int V = scanner.nextInt();
        int E = scanner.nextInt();
        scanner.nextLine();

        Kruskal kruskal = new Kruskal(V);
        for (int i = 0; i < E; i++) {
            int[] e = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
            kruskal.addEdge(e[0], e[1], e[2]);
        }

        List<Edge> usedEdges = kruskal.run();
        System.out.println(usedEdges.stream().mapToLong(x -> x.cost).sum());
    }
}

class UnionFindTree {
    int size;
    int[] par;
    int[] rank;

    public UnionFindTree(int size) {
        this.size = size;
        par = new int[size];
        for (int i = 0; i < size; i++) {
            par[i] = i;
        }
        rank = new int[size];
        Arrays.fill(rank, 1);
    }

    private int root(int x) {
        if (x == par[x]) {
            return x;
        }
        int root = root(par[x]);
        par[x] = root;
        return root;
    }

    public boolean same(int x, int y) {
        return root(x) == root(y);
    }

    public void merge(int x, int y) {
        int xr = root(x);
        int yr = root(y);
        if (rank[xr] < rank[yr]) {
            par[xr] = yr;
        } else {
            par[yr] = xr;
            if (rank[xr] == rank[yr]) {
                rank[xr]++;
            }
        }
    }

}

class Kruskal {
    private int size;
    Queue<Edge> edges;
    UnionFindTree uft;

    public Kruskal(int size) {
        this.size = size;
        edges = new PriorityQueue<>(Comparator.comparingInt(x -> x.cost));
        uft = new UnionFindTree(size);
    }

    public void addEdge(int from, int to, int cost) {
        edges.add(new Edge(from, to, cost));
    }

    public List<Edge> run() {
        List<Edge> usedEdges = new ArrayList<>();
        while (!edges.isEmpty()) {
            Edge e = edges.poll();
            if (uft.same(e.from, e.to)) {
                continue;
            }
            uft.merge(e.from, e.to);
            usedEdges.add(e);
        }
        return usedEdges;
    }

}


class Edge {
    public final int from;
    public final int to;
    public final int cost;

    public Edge(int from, int to, int cost) {
        this.from = from;
        this.to = to;
        this.cost = cost;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Edge edge = (Edge) o;
        return from == edge.from &&
                to == edge.to &&
                cost == edge.cost;
    }

    @Override
    public int hashCode() {
        return Objects.hash(from, to, cost);
    }
}