from collections import defaultdict

import heapq



vertices,edges = list(map(int,input().split(" ")))

link = defaultdict(dict)

weight = defaultdict(dict)

way = [float("inf")] * vertices

for e in range(edges):

    source,target,w = list(map(int,input().split(" ")))

    link[source] = link.get(source,set()) | {target}

    link[target] = link.get(target,set()) | {source}

    weight[source].update({target:w})

    weight[target].update({source:w})

queue = []

heapq.heapify(queue)

heapq.heappush(queue,[0,0])

went = [0] * vertices

now_weight = 0

way[0] = 0



while queue:

    here_weight,here = heapq.heappop(queue)

    #print(went)

    if went[here]:

        continue

    went[here] = 1

    now_weight += here_weight

    min_weight,next_t = float("inf"),-1

    for c in link[here]:

        if not went[c] and way[c] > weight[here][c]:

            heapq.heappush(queue,[weight[here][c],c])

            way[c] = weight[here][c]

    #print(queue)

print(now_weight)
from collections import defaultdict

import heapq

vertices, edges = (int(n) for n in input().split(" "))

conn = defaultdict(set)

weight = defaultdict(dict)

for i in range(edges):

    v1, v2, w = (int(n) for n in input().split(" "))

    conn[v1] |= {v2}

    conn[v2] |= {v1}

    weight[v1][v2] = w

    weight[v2][v1] = w



queue = []

heapq.heapify(queue)

heapq.heappush(queue, [0,0])

went = set()

went_vertices = 0

answer_weight = 0

while went_vertices < vertices:

    here_weight, here_v = heapq.heappop(queue)

    if here_v in went:

        continue

    went |= {here_v}

    for c in conn[here_v]:

        if c not in went:

            heapq.heappush(queue,[weight[here_v][c], c])

    answer_weight += here_weight

    went_vertices += 1

print(answer_weight)
from collections import defaultdict
import heapq

vertices, edges = list(map(int, input().split(" ")))
link = defaultdict(dict)
weight = defaultdict(dict)
way = [float("inf")] * vertices
for e in range(edges):
    source, target, w = list(map(int, input().split(" ")))
    link[source] = link.get(source, set()) | {target}
    link[target] = link.get(target, set()) | {source}
    weight[source].update({target: w})
    weight[target].update({source: w})
queue = []
heapq.heapify(queue)
heapq.heappush(queue, [0, 0])
went = [0] * vertices
now_weight = 0
way[0] = 0
while queue:
    here_weight, here = heapq.heappop(queue)
    # print(went)
    if went[here]:
        continue
    went[here] = 1
    now_weight += here_weight
    min_weight, next_t = float("inf"), -1
    for c in link[here]:
        if not went[c] and way[c] > weight[here][c]:
            heapq.heappush(queue, [weight[here][c], c])
            way[c] = weight[here][c]
    # print(queue)
print(now_weight)

from collections import defaultdict
import heapq

vertices, edges = (int(n) for n in input().split(" "))
conn = defaultdict(set)
weight = defaultdict(dict)
for i in range(edges):
    v1, v2, w = (int(n) for n in input().split(" "))
    conn[v1] |= {v2}
    conn[v2] |= {v1}
    weight[v1][v2] = w
    weight[v2][v1] = w
queue = []
heapq.heapify(queue)
heapq.heappush(queue, [0, 0])
went = set()
went_vertices = 0
answer_weight = 0
while went_vertices < vertices:
    here_weight, here_v = heapq.heappop(queue)
    if here_v in went:
        continue
    went |= {here_v}
    for c in conn[here_v]:
        if c not in went:
            heapq.heappush(queue, [weight[here_v][c], c])
    answer_weight += here_weight
    went_vertices += 1
print(answer_weight)

-vertices, edges = list(map(int, input().split(" "))) -link = defaultdict(dict) +vertices, edges = (int(n) for n in input().split(" ")) +conn = defaultdict(set) -way = [float("inf")] * vertices -for e in range(edges): -    source, target, w = list(map(int, input().split(" "))) -    link[source] = link.get(source, set()) | {target} -    link[target] = link.get(target, set()) | {source} -    weight[source].update({target: w}) -    weight[target].update({source: w}) +for i in range(edges): +    v1, v2, w = (int(n) for n in input().split(" ")) +    conn[v1] |= {v2} +    conn[v2] |= {v1} +    weight[v1][v2] = w +    weight[v2][v1] = w -went = [0] * vertices -now_weight = 0 -way[0] = 0 -while queue: -    here_weight, here = heapq.heappop(queue) -    # print(went) -    if went[here]: +went = set() +went_vertices = 0 +answer_weight = 0 +while went_vertices < vertices: +    here_weight, here_v = heapq.heappop(queue) +    if here_v in went: -    went[here] = 1 -    now_weight += here_weight -    min_weight, next_t = float("inf"), -1 -    for c in link[here]: -        if not went[c] and way[c] > weight[here][c]: -            heapq.heappush(queue, [weight[here][c], c]) -            way[c] = weight[here][c] -    # print(queue) -print(now_weight) +    went |= {here_v} +    for c in conn[here_v]: +        if c not in went: +            heapq.heappush(queue, [weight[here_v][c], c]) +    answer_weight += here_weight +    went_vertices += 1 +print(answer_weight)