class UnionFind(): def __init__(self, n): self.data = [-1 for _ in range(n)] def find(self, x): stack = [] while self.data[x] >= 0: stack.append(x) x = self.data[x] while len(stack): y = stack.pop() self.data[y] = x return x def unite(self, x, y): x, y = self.find(x), self.find(y) if x == y: return if self.data[x] > self.data[y]: x, y = y, x self.data[x] += self.data[y] self.data[y] = x return def same(self, x, y): return self.find(x) == self.find(y) def size(self, x): return -self.data[self.find(x)] N, M = map(int, input().split()) E = [] for _ in range(M): a, b, c = map(int, input().split()) E.append((c, a - 1, b - 1)) E.sort() ans = 0 uf = UnionFind(N) while len(E): c, a, b = E.pop() if uf.same(a, b): continue uf.unite(a, b) ans += c print(ans * 2)