import java.io.*;
import java.util.Arrays;
public class Main {
static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public static final int N = 5010, INF = 0x3f3f3f3f;
static int n, m;
static int[][] g = new int[N][N];
static int[] dist = new int[N];
static boolean[] vis = new boolean[N];
public static void main(String[] args) throws Exception{
n = getInt(); m = getInt();
for (int[] gi : g) Arrays.fill(gi, (int) 1e10);
while (m-- > 0) {
int a = getInt(), b = getInt(), c = getInt();
g[a][b] = g[b][a] = Math.min(g[a][b], c);
}
long t = prim();
if (t == INF) pw.println("orz");
else pw.println(t);
pw.flush();
}
public static long prim() {
Arrays.fill(dist, (int) 1e10);
long res = 0;
for (int i = 0; i < n; i++) {
int t = -1;
for (int j = 1; j <= n; j++) {
if (!vis[j] && (t == -1 || dist[t] > dist[j])) {
t = j;
}
}
if (i != 0 && dist[t] == INF) return INF;
if (i != 0) res += dist[t];
for (int j = 1; j <= n; j++) {
dist[j] = Math.min(dist[j], g[t][j]);
}
vis[t] = true;
}
return res;
}
public static int getInt() throws Exception {
st.nextToken();
return (int) st.nval;
}
}