'''
Prim 最小生成树算法
'''
# 普利姆算法求解最小生成树输入为边列表,每条边表示为(起点,终点,权重)
# 返回最小生成树权重总和,以及最小生成树边列表, 生成不了最小生成树返回None
from queue import PriorityQueue
def getMinSpanTreePrim(edges):
if len(edges) == 0:
return [0, []]
span_tree = []
node_set = set()
link = {}
for a, b, w in edges:
node_set.add(a)
node_set.add(b)
if a not in link:
link[a] = []
if b not in link:
link[b] = []
link[a].append((b, w))
link[b].append((a, w))
node = None
sum = 0
min_heap = PriorityQueue()
visited = set()
for _ in range(len(node_set)):
if node is None:
node = edges[0][0]
visited.add(node)
for next, w in link[node]:
min_heap.put((w, node, next))
else:
while True:
if min_heap.empty():
return [None, None]
w, node1, node2 = min_heap.get()
if not (node1 in visited and node2 in visited):
sum += w
span_tree.append((node1, node2))
for node in [node1, node2]:
if node not in visited:
visited.add(node)
for next, w in link[node]:
min_heap.put((w, node, next))
break
return [sum, span_tree] if len(span_tree) == len(node_set) - 1 else [None, None]
n, m = map(int, input().split())
edges = []
for _ in range(m):
a, b, w = map(int, input().split())
edges.append((a, b, w))
ans = getMinSpanTreePrim(edges)
print(ans[0] if ans[0] is not None else 'impossible')