#include <bits/stdc++.h>
using namespace std;
long long cost[15][15];
long long mincost[15];
bool used[15];
long long V;
long long prim() {
  for (long long i = 0; i < V; ++i) {
    mincost[i] = 1e+9;
    used[i] = false;
  }
  mincost[0] = 0;
  long long res = 0;
  while (true) {
    long long v = -1;
    for (long long u = 0; u < V; ++u) {
      if (!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
    }
    if (v == -1) break;
    used[v] = true;
    res += mincost[v];
    for (long long u = 0; u < V; ++u) {
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }
  return res;
}
auto main() -> signed {
  cin >> V;
  long long m;
  cin >> m;
  for (long long i = 0; i < m; ++i) {
    long long a, b, c;
    cin >> a >> b >> c;
    cost[a][b] = c;
    cost[b][a] = c;
  }
  long long ans = prim();
  cout << ans << endl;
  return 0;
}
