import sys

input = lambda: sys.stdin.readline().rstrip()

# 서로소 집합(disjoint set)
parent = {}
rank = {}


def make_set(v):
    parent[v] = v
    rank[v] = 0


def find(v):
    if parent[v] != v:
        parent[v] = find(parent[v])

    return parent[v]


def union(v, u):
    r1 = find(v)
    r2 = find(u)

    if r1 != r2:
        if rank[r1] > rank[r2]:
            parent[r2] = r1
        else:
            parent[r1] = r2

            if rank[r1] == rank[r2]:
                rank[r2] += 1


# 크러스컬 알고리듬(Kruskal’s algorithm)
def weight_of_mst_kruskal():
    v, e = map(int, input().split())

    # 간선 가중치 오름차순으로 정렬
    edges = []
    for _ in range(e):
        a, b, c = tuple(map(int, input().split()))
        edges.append((c, a, b))
    edges.sort()

    # 정점 트리
    for node in range(1, v + 1):
        make_set(node)

    # 서로소 집합으로 MST에 포함되는 간선인지 판단
    # mst = []
    result = 0

    for edge in edges:
        c, a, b = edge

        if find(a) != find(b):
            union(a, b)
            # mst.append(edge)
            result += c

    # return mst
    return result


print(weight_of_mst_kruskal())
