import java.io.*;
import java.util.*;
class Edge {
int a, b, w;
public Edge(int a, int b, int w) {
this.a = a;
this.b = b;
this.w = w;
}
}
public class Main {
static final int N = 200010;
static int[] p = new int[N];
static int n, m;
static int u, v, w;
static Edge[] edge = new Edge[N];
static void sort(Edge[] edge, int l, int r) {
if (l >= r) return;
int x = edge[l + r >> 1].w, i = l - 1, j = r + 1;
while (i < j) {
do i++; while (edge[i].w < x);
do j--; while (edge[j].w > x);
if (i < j) {
Edge e = edge[i];
edge[i] = edge[j];
edge[j] = e;
}
}
sort(edge, l, j);
sort(edge, j + 1, r);
}
static int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
static int Kruskal() {
int res = 0, cnt = 0;
sort(edge, 0, m - 1);
for (int i = 0; i < m; i++) {
int a = edge[i].a;
int b = edge[i].b;
int w = edge[i].w;
a = find(a);
b = find(b);
if (a != b) {
p[a] = b;
cnt++;
res += w;
}
}
if (cnt < n - 1) return 0x3f3f3f3f;
return res;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] first_line = br.readLine().split(" ");
n = Integer.parseInt(first_line[0]);
m = Integer.parseInt(first_line[1]);
for (int i = 0; i < m; i++) {
String[] second_line = br.readLine().split(" ");
u = Integer.parseInt(second_line[0]);
v = Integer.parseInt(second_line[1]);
w = Integer.parseInt(second_line[2]);
edge[i] = new Edge(u, v, w);
}
for (int i = 1; i <= n; i++) p[i] = i;
int ans = Kruskal();
if (ans == 0x3f3f3f3f) {
bw.write("impossible\n");
bw.flush();
}
else {
bw.write(ans + "\n");
bw.flush();
}
}
}