#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 5;
int pset[MAX], nodes[MAX], edges[MAX];
int n, m;
int find_set(int i) { return pset[i] == i ? i : pset[i] = find_set(pset[i]); }
inline bool same_set(int i, int j) { return find_set(i) == find_set(j); }
int main() {
  scanf("%d %d", &n, &m);
  vector<tuple<int, int, int> > e;
  for (int i = int(0); i < int(m); i++) {
    int a, b, w;
    scanf("%d %d %d", &a, &b, &w);
    e.emplace_back(w, a, b);
  }
  for (int i = int(1); i < int(n + 1); i++) {
    pset[i] = i;
    nodes[i] = 1;
  }
  long long ans = 0;
  sort(e.begin(), e.end(), greater<tuple<int, int, int> >());
  for (auto &each : e) {
    int w, a, b;
    tie(w, a, b) = each;
    if (!same_set(a, b)) {
      int pa = find_set(a);
      int pb = find_set(b);
      if (nodes[pa] + nodes[pb] >= edges[pa] + edges[pb] + 1) {
        nodes[pb] += nodes[pa];
        edges[pb] += edges[pa] + 1;
        pset[pa] = pb;
        ans += w;
      }
    } else {
      int pa = find_set(a);
      if (nodes[pa] > edges[pa]) {
        edges[pa]++;
        ans += w;
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
