import sys

readline = sys.stdin.readline





class UnionFind:

    __slots__ = ["nodes"]



    def __init__(self, n: int) -> None:

        self.nodes = [-1]*n



    def get_root(self, x: int) -> int:

        if self.nodes[x] < 0:

            return x

        else:

            self.nodes[x] = self.get_root(self.nodes[x])

            return self.nodes[x]



    def unite(self, x: int, y: int) -> bool:

        root_x, root_y = self.get_root(x), self.get_root(y)

        if root_x != root_y:

            bigroot, smallroot = \

                (root_x, root_y) if self.nodes[root_x] < self.nodes[root_y] else (root_y, root_x)

            self.nodes[bigroot] += self.nodes[smallroot]

            self.nodes[smallroot] = bigroot

            return True

        else:

            return False





def kruskal(n: int, edges: list) -> int:

    edges.sort()

    tree = UnionFind(n)

    total = 0

    cnt = 0

    for w, s, t in edges:

        if tree.unite(s, t):

            cnt += 1

            total += w

            if cnt == n - 1:

                break



    return total





V, E = map(int, readline().split())

edges = []

append = edges.append

for _ in [None]*E:

    s, t, w = map(int, readline().split())

    append((w, s, t))

print(kruskal(V, edges))

import sys



def kruskal(n: int, edges: list) -> int:

    tree = [-1]*n



    def get_root(x):

        if tree[x] < 0:

            return x

        tree[x] = get_root(tree[x])

        return tree[x]



    def unite(x, y):

        x, y = get_root(x), get_root(y)

        if x != y:

            big, small = (x, y) if tree[x] < tree[y] else (y, x)

            tree[big] += tree[small]

            tree[small] = big

        return x != y



    return sum(w for (w, _, _), _ in zip([p for p in sorted(edges) if unite(p[1], p[2])], list(range(n-1))))



V, E = list(map(int, input().split()))

edges = [(w, s, t) for s, t, w in (list(map(int, l.split())) for l in sys.stdin)]

print((kruskal(V, edges)))

import sys

readline = sys.stdin.readline


class UnionFind:
    __slots__ = ["nodes"]

    def __init__(self, n: int) -> None:
        self.nodes = [-1] * n

    def get_root(self, x: int) -> int:
        if self.nodes[x] < 0:
            return x
        else:
            self.nodes[x] = self.get_root(self.nodes[x])
            return self.nodes[x]

    def unite(self, x: int, y: int) -> bool:
        root_x, root_y = self.get_root(x), self.get_root(y)
        if root_x != root_y:
            bigroot, smallroot = (
                (root_x, root_y)
                if self.nodes[root_x] < self.nodes[root_y]
                else (root_y, root_x)
            )
            self.nodes[bigroot] += self.nodes[smallroot]
            self.nodes[smallroot] = bigroot
            return True
        else:
            return False


def kruskal(n: int, edges: list) -> int:
    edges.sort()
    tree = UnionFind(n)
    total = 0
    cnt = 0
    for w, s, t in edges:
        if tree.unite(s, t):
            cnt += 1
            total += w
            if cnt == n - 1:
                break
    return total


V, E = map(int, readline().split())
edges = []
append = edges.append
for _ in [None] * E:
    s, t, w = map(int, readline().split())
    append((w, s, t))
print(kruskal(V, edges))

import sys


def kruskal(n: int, edges: list) -> int:
    tree = [-1] * n

    def get_root(x):
        if tree[x] < 0:
            return x
        tree[x] = get_root(tree[x])
        return tree[x]

    def unite(x, y):
        x, y = get_root(x), get_root(y)
        if x != y:
            big, small = (x, y) if tree[x] < tree[y] else (y, x)
            tree[big] += tree[small]
            tree[small] = big
        return x != y

    return sum(
        w
        for (w, _, _), _ in zip(
            [p for p in sorted(edges) if unite(p[1], p[2])], list(range(n - 1))
        )
    )


V, E = list(map(int, input().split()))
edges = [(w, s, t) for s, t, w in (list(map(int, l.split())) for l in sys.stdin)]
print((kruskal(V, edges)))

- -readline = sys.stdin.readline - - -class UnionFind: -    __slots__ = ["nodes"] - -    def __init__(self, n: int) -> None: -        self.nodes = [-1] * n - -    def get_root(self, x: int) -> int: -        if self.nodes[x] < 0: -            return x -        else: -            self.nodes[x] = self.get_root(self.nodes[x]) -            return self.nodes[x] - -    def unite(self, x: int, y: int) -> bool: -        root_x, root_y = self.get_root(x), self.get_root(y) -        if root_x != root_y: -            bigroot, smallroot = ( -                (root_x, root_y) -                if self.nodes[root_x] < self.nodes[root_y] -                else (root_y, root_x) -            ) -            self.nodes[bigroot] += self.nodes[smallroot] -            self.nodes[smallroot] = bigroot -            return True -        else: -            return False -    edges.sort() -    tree = UnionFind(n) -    total = 0 -    cnt = 0 -    for w, s, t in edges: -        if tree.unite(s, t): -            cnt += 1 -            total += w -            if cnt == n - 1: -                break -    return total +    tree = [-1] * n + +    def get_root(x): +        if tree[x] < 0: +            return x +        tree[x] = get_root(tree[x]) +        return tree[x] + +    def unite(x, y): +        x, y = get_root(x), get_root(y) +        if x != y: +            big, small = (x, y) if tree[x] < tree[y] else (y, x) +            tree[big] += tree[small] +            tree[small] = big +        return x != y + +    return sum( +        w +        for (w, _, _), _ in zip( +            [p for p in sorted(edges) if unite(p[1], p[2])], list(range(n - 1)) +        ) +    ) -V, E = map(int, readline().split()) -edges = [] -append = edges.append -for _ in [None] * E: -    s, t, w = map(int, readline().split()) -    append((w, s, t)) -print(kruskal(V, edges)) +V, E = list(map(int, input().split())) +edges = [(w, s, t) for s, t, w in (list(map(int, l.split())) for l in sys.stdin)] +print((kruskal(V, edges)))