#include <bits/stdc++.h>
using namespace std;
const long long N = 555;
long long n, m, a[N], b[N], c[N], tot = 0, p = 0, fa[N], now = 0, ans, sum = 0,
                                  vis[N];
struct E {
  long long u, v, w, id;
} e[N];
bool cmp(const E x, const E y) { return x.w > y.w; }
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return f * x;
}
inline void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
  return;
}
inline long long getfa(long long x) {
  return (x == fa[x]) ? x : (fa[x] = getfa(fa[x]));
}
int main() {
  n = read(), m = read();
  for (register long long i = (1); i <= (m); i++)
    a[i] = read(), b[i] = read(), c[i] = read(),
    e[i] = (E){a[i], b[i], c[i], i}, tot += c[i];
  sort(e + 1, e + m + 1, cmp);
  for (register long long i = (1); i <= (n); i++) fa[i] = i;
  while (now < n - 1) {
    p++;
    long long u = e[p].u, v = e[p].v, w = e[p].w;
    long long fu = getfa(u), fv = getfa(v);
    if (fu == fv) continue;
    fa[fu] = fv;
    sum += w;
    now++;
    vis[e[p].id] = 1;
  }
  ans = tot - sum;
  write(ans);
  return 0;
}
