v,e = list(map(int, input().split()))

adj = [list(map(int, input().split())) for i in range(e)]

adj.sort(key = lambda x:x[2])

group = [[i] for i in range(v)]

key = [i for i in range(v)]

sum = 0

for i,j,k in adj:

    if key[i] != key[j]:

        h = key[j]

        group[key[i]] += group[h]

        sum += k

        for t in group[h]:

            key[t] = key[i]

        group[h] = []

        if len(group[key[i]]) == v:

            break

print(sum)

def root(x):

    if par[x] == x:

        return x

    par[x] = root(par[x])

    return par[x]



def same(x, y):

    return root(x) == root(y)



def unite(x, y):

    x = root(x)

    y = root(y)

    par[x] = y



v,e = list(map(int, input().split()))

adj = [list(map(int, input().split())) for i in range(e)]

adj.sort(key = lambda x:x[2])

par = [i for i in range(v)]

sum = 0



for i,j,k in adj:

    if not same(i, j):

        sum += k

        unite(i, j)

print(sum)

v, e = list(map(int, input().split()))
adj = [list(map(int, input().split())) for i in range(e)]
adj.sort(key=lambda x: x[2])
group = [[i] for i in range(v)]
key = [i for i in range(v)]
sum = 0
for i, j, k in adj:
    if key[i] != key[j]:
        h = key[j]
        group[key[i]] += group[h]
        sum += k
        for t in group[h]:
            key[t] = key[i]
        group[h] = []
        if len(group[key[i]]) == v:
            break
print(sum)

def root(x):
    if par[x] == x:
        return x
    par[x] = root(par[x])
    return par[x]


def same(x, y):
    return root(x) == root(y)


def unite(x, y):
    x = root(x)
    y = root(y)
    par[x] = y


v, e = list(map(int, input().split()))
adj = [list(map(int, input().split())) for i in range(e)]
adj.sort(key=lambda x: x[2])
par = [i for i in range(v)]
sum = 0
for i, j, k in adj:
    if not same(i, j):
        sum += k
        unite(i, j)
print(sum)

+def root(x): +    if par[x] == x: +        return x +    par[x] = root(par[x]) +    return par[x] + + +def same(x, y): +    return root(x) == root(y) + + +def unite(x, y): +    x = root(x) +    y = root(y) +    par[x] = y + + -group = [[i] for i in range(v)] -key = [i for i in range(v)] +par = [i for i in range(v)] -    if key[i] != key[j]: -        h = key[j] -        group[key[i]] += group[h] +    if not same(i, j): -        for t in group[h]: -            key[t] = key[i] -        group[h] = [] -        if len(group[key[i]]) == v: -            break +        unite(i, j)