import heapq
N, W = map(int, input().split())

graph = [[] for i in range(N)]

for i in range(W):
    x, y, z = map(int, input().split())
    x -= 1
    y -= 1
    graph[x].append((y, z))
    graph[y].append((x, z))

def Prim():
    used = [False]*N
    ans = 0
    edgelist = []
    for e in graph[0]:
        heapq.heappush(edgelist, e[::-1])
    used[0] = True 
    v = 1
    while v < N:
        minedge = heapq.heappop(edgelist)
        if used[minedge[1]]:
            continue
        ans += minedge[0]
        v += 1
        used[minedge[1]] = True
        for e in graph[minedge[1]]:
            if not used[e[0]]:
                heapq.heappush(edgelist, e[::-1])
    return ans

print(W-Prim())