function Main(input) {
	input = input.trim().split("\n").map(function(x) { return x.split(" ")});
	let lVl = parseInt(input[0][0], 10);
	let lEl = parseInt(input[0][1], 10);
	let graph = [];
	for (let i = 0; i < lEl; i++){
		graph.push(input[i + 1].map(e => parseInt(e, 10)));
	}
	graph.sort((a, b) => a[2] - b [2]);

	let uf = new UnionFind(lVl);
	let ans = 0;
	for (let i = 0; i < graph.length; i++){
		if (uf.find(graph[i][0]) !== uf.find(graph[i][1])) {
			uf.union(graph[i][0], graph[i][1])
			ans += graph[i][2];
		}
	}

	console.log(ans);

}

function UnionFind (n) {
    this.parents = [];
    for (let i = 0; i < n; i++) {
        this.parents.push(-1);
    }
}

UnionFind.prototype.find = function (x) {
    if (this.parents[x] < 0) return x;
    return this.parents[x] = this.find(this.parents[x]);    
}


UnionFind.prototype.union = function (x, y) {
    x = this.find(x);
    y = this.find(y);
    if (x === y) return;
    if (this.parents[x] > this.parents[y]) [x, y] = [y, x];
    
    this.parents[x] += this.parents[y];
    this.parents[y] = x;
}


UnionFind.prototype.size = function (x) {
    x = this.find(x);
    while (x >= 0) {
        x = this.parents[x];
    }
    return x * -1;
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));

