import sys



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

    tree = [-1]*v_count



    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



    cost = 0

    for (w, _s, _t), _ in zip([p for p in sorted(edges) if unite(*p[1:])],

                              list(range(v_count-1))):

        cost += w

    return cost



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

from itertools import islice



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

    tree = [-1]*v_count



    def get_root(x):

        if tree[x] < 0:

            return x

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

        return tree[x]



    def unite(a):

        x, y = get_root(a[1]), get_root(a[2])

        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



    cost = 0

    for w, _s, _t in islice(list(filter(unite, sorted(edges))), v_count-1):

        cost += w

    return cost



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


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

    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

    cost = 0
    for (w, _s, _t), _ in zip(
        [p for p in sorted(edges) if unite(*p[1:])], list(range(v_count - 1))
    ):
        cost += w
    return cost


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
from itertools import islice


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

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

    def unite(a):
        x, y = get_root(a[1]), get_root(a[2])
        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

    cost = 0
    for w, _s, _t in islice(list(filter(unite, sorted(edges))), v_count - 1):
        cost += w
    return cost


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)))

+from itertools import islice -    def unite(x, y): -        x, y = get_root(x), get_root(y) +    def unite(a): +        x, y = get_root(a[1]), get_root(a[2]) -    for (w, _s, _t), _ in zip( -        [p for p in sorted(edges) if unite(*p[1:])], list(range(v_count - 1)) -    ): +    for w, _s, _t in islice(list(filter(unite, sorted(edges))), v_count - 1):