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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(in, out);
        out.close();
    }

    static class Task {
        int N, M;

        void solve(InputReader in, PrintWriter out) {
            N = in.nextInt();
            M = in.nextInt();

            Kruskal ksk = new Kruskal(N);

            for (int i = 0; i < M; ++i) {
                int u = in.nextInt();
                int v = in.nextInt();
                int w = in.nextInt();
                ksk.addEdge(u, v, w);
            }

            ksk.setMST();
            out.println(ksk.getTotalCost());
        }
    }

    static class Kruskal {
        private int totalCost;

        private UnionFind uf;
        private List<Edge> edges;
        private List<Edge> mst;

        public Kruskal(int n) {
            this.totalCost = 0;
            this.uf = new UnionFind(n);
            this.edges = new ArrayList<>();
            this.mst = new ArrayList<>();
        }

        public void setMST() {
            Collections.sort(edges);
            for (Edge e : edges) {
                if (!uf.same(e.src, e.dest)) {
                    uf.unite(e.src, e.dest);
                    totalCost += e.weight;
                    mst.add(e);
                }
            }
        }

        public int getTotalCost() {
            return totalCost;
        }

        public List<Edge> getEdges() {
            return mst;
        }

        public void addEdge(int u, int v, int w) {
            edges.add(new Edge(u, v, w));
        }

        private class Edge implements Comparable<Edge> {
            int src;
            int dest;
            int weight;

            public Edge(int s, int d, int w) {
                this.src = s;
                this.dest = d;
                this.weight = w;
            }

            @Override
            public int compareTo(Edge e) {
                return this.weight - e.weight;
            }
        }
        private class UnionFind {
            private int[] rank, parent;

            public UnionFind(int size) {
                this.rank = new int[size];
                this.parent = new int[size];
                for (int i = 0; i < size; ++i) {
                    parent[i] = i;
                    rank[i] = 0;
                }
            }

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

            public void unite(int x, int y) {
                int a = root(x);
                int b = root(y);

                if (rank[a] < rank[b]) {
                    int tmp = a;
                    a = b;
                    b = tmp;
                }

                parent[b] = a;
                if (rank[a] == rank[b])
                    rank[a]++;
            }

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

    static class InputReader {
        static int BUFSIZE = 32768;

        BufferedReader reader;
        StringTokenizer tokenizer;

        InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), BUFSIZE);
            tokenizer = null;
        }

        String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        int[] nextIntArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; ++i) {
                arr[i] = nextInt();
            }
            return arr;
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        long[] nextLongArray(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; ++i) {
                arr[i] = nextLong();
            }
            return arr;
        }
    }
}