import array

import collections

import heapq



AdjacentVertex = collections.namedtuple("AdjacentVertex", "vertex cost")

INF = 2 ** 31 - 1

NO_VERTEX = -1

 

# Prim?????§??????0?????????????°???¨?????¨????±???????

def compute_mst_prim(max_v, adj_list):

    # pred[u]?????????u???????????¨???????????????????????¨???

    pred = collections.defaultdict(lambda: NO_VERTEX)

    # u??¨pred[u]????????¶???????????????key[u]?????\???

    key = collections.defaultdict(lambda: INF)

    key[0] = 0

    # ??????????????????????????????????????\?????¨????????¨??????

    pq = [(key[v], v) for v in range(max_v)]

    heapq.heapify(pq)

    # ????????????????????\?????????????????\??£???????????????????????????

    in_pq = array.array("B", (True for _ in range(max_v)))

    while pq:

        _, u = heapq.heappop(pq)

        in_pq[u] = False

        for v, v_cost in adj_list[u]:

            if in_pq[v]:

                weight = v_cost

                if weight < key[v]:

                    pred[v] = u

                    key[v] = weight

                    heapq.heappush(pq, (weight, v))

                    in_pq[v] = True

    return (pred, key)



max_v, max_e = list(map(int, input().split()))

adjacency_list = collections.defaultdict(set)

for _ in range(max_e):

    s, t, w = list(map(int, input().split()))

    adjacency_list[s].add(AdjacentVertex(t, w))

    adjacency_list[t].add(AdjacentVertex(s, w))

(_, key) = compute_mst_prim(max_v, adjacency_list)



print((sum(key.values())))
import heapq

from collections import defaultdict



INF = float('inf')





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

Adj_list = defaultdict(set)



for i in range(E):

    s, t, w = list(map(int, input().split()))

    Adj_list[s].add((t, w))

    Adj_list[t].add((s, w))

    

def prim(x):

    d = [INF] * x

    d[0] = 0

    isVisited = [False] * x

    hq = [(0,0)] 

    

    while hq:

        u = heapq.heappop(hq)[1]

        isVisited[u] = True

        

        for v, c in Adj_list[u]:

            if not isVisited[v] and d[v] > c:

                d[v] = c

                heapq.heappush(hq, (c, v))

    return sum(d)



print((prim(V)))

    
import array
import collections
import heapq

AdjacentVertex = collections.namedtuple("AdjacentVertex", "vertex cost")
INF = 2**31 - 1
NO_VERTEX = -1
# Prim?????§??????0?????????????°???¨?????¨????±???????
def compute_mst_prim(max_v, adj_list):
    # pred[u]?????????u???????????¨???????????????????????¨???
    pred = collections.defaultdict(lambda: NO_VERTEX)
    # u??¨pred[u]????????¶???????????????key[u]?????\???
    key = collections.defaultdict(lambda: INF)
    key[0] = 0
    # ??????????????????????????????????????\?????¨????????¨??????
    pq = [(key[v], v) for v in range(max_v)]
    heapq.heapify(pq)
    # ????????????????????\?????????????????\??£???????????????????????????
    in_pq = array.array("B", (True for _ in range(max_v)))
    while pq:
        _, u = heapq.heappop(pq)
        in_pq[u] = False
        for v, v_cost in adj_list[u]:
            if in_pq[v]:
                weight = v_cost
                if weight < key[v]:
                    pred[v] = u
                    key[v] = weight
                    heapq.heappush(pq, (weight, v))
                    in_pq[v] = True
    return (pred, key)


max_v, max_e = list(map(int, input().split()))
adjacency_list = collections.defaultdict(set)
for _ in range(max_e):
    s, t, w = list(map(int, input().split()))
    adjacency_list[s].add(AdjacentVertex(t, w))
    adjacency_list[t].add(AdjacentVertex(s, w))
(_, key) = compute_mst_prim(max_v, adjacency_list)
print((sum(key.values())))

import heapq
from collections import defaultdict

INF = float("inf")
V, E = list(map(int, input().split()))
Adj_list = defaultdict(set)
for i in range(E):
    s, t, w = list(map(int, input().split()))
    Adj_list[s].add((t, w))
    Adj_list[t].add((s, w))


def prim(x):
    d = [INF] * x
    d[0] = 0
    isVisited = [False] * x
    hq = [(0, 0)]
    while hq:
        u = heapq.heappop(hq)[1]
        isVisited[u] = True
        for v, c in Adj_list[u]:
            if not isVisited[v] and d[v] > c:
                d[v] = c
                heapq.heappush(hq, (c, v))
    return sum(d)


print((prim(V)))

-import array -import collections +from collections import defaultdict -AdjacentVertex = collections.namedtuple("AdjacentVertex", "vertex cost") -INF = 2**31 - 1 -NO_VERTEX = -1 -# Prim?????§??????0?????????????°???¨?????¨????±??????? -def compute_mst_prim(max_v, adj_list): -    # pred[u]?????????u???????????¨???????????????????????¨??? -    pred = collections.defaultdict(lambda: NO_VERTEX) -    # u??¨pred[u]????????¶???????????????key[u]?????\??? -    key = collections.defaultdict(lambda: INF) -    key[0] = 0 -    # ??????????????????????????????????????\?????¨????????¨?????? -    pq = [(key[v], v) for v in range(max_v)] -    heapq.heapify(pq) -    # ????????????????????\?????????????????\??£??????????????????????????? -    in_pq = array.array("B", (True for _ in range(max_v))) -    while pq: -        _, u = heapq.heappop(pq) -        in_pq[u] = False -        for v, v_cost in adj_list[u]: -            if in_pq[v]: -                weight = v_cost -                if weight < key[v]: -                    pred[v] = u -                    key[v] = weight -                    heapq.heappush(pq, (weight, v)) -                    in_pq[v] = True -    return (pred, key) +INF = float("inf") +V, E = list(map(int, input().split())) +Adj_list = defaultdict(set) +for i in range(E): +    s, t, w = list(map(int, input().split())) +    Adj_list[s].add((t, w)) +    Adj_list[t].add((s, w)) -max_v, max_e = list(map(int, input().split())) -adjacency_list = collections.defaultdict(set) -for _ in range(max_e): -    s, t, w = list(map(int, input().split())) -    adjacency_list[s].add(AdjacentVertex(t, w)) -    adjacency_list[t].add(AdjacentVertex(s, w)) -(_, key) = compute_mst_prim(max_v, adjacency_list) -print((sum(key.values()))) +def prim(x): +    d = [INF] * x +    d[0] = 0 +    isVisited = [False] * x +    hq = [(0, 0)] +    while hq: +        u = heapq.heappop(hq)[1] +        isVisited[u] = True +        for v, c in Adj_list[u]: +            if not isVisited[v] and d[v] > c: +                d[v] = c +                heapq.heappush(hq, (c, v)) +    return sum(d) + + +print((prim(V)))