#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T& t) {
  cin >> t;
}
template <typename T, typename... U>
void read(T& t, U&... u) {
  read(t);
  read(u...);
}
template <typename T>
void write(T t) {
  cout << t;
}
template <typename T, typename... U>
void write(T t, U... u) {
  write(t);
  write(u...);
}
const vector<pair<int, int>> dxy = {pair<int, int>(0, -1), pair<int, int>(0, 1),
                                    pair<int, int>(-1, 0),
                                    pair<int, int>(1, 0)};
const long double EPS = 1e-9L, PI = acos(-1.0L);
const long long INFLL = 4e18L;
const int INF = 2e9, MOD = 1e9 + 7, LOGN = 16 + 1;
const int N = 2e5 + 3, N2 = +3;
struct DSU {
  vector<int> rep, size, nbe;
  DSU() {}
  DSU(int n) {
    rep.assign(n + 1, 0);
    size.assign(n + 1, 1);
    nbe.assign(n + 1, 0);
    for (int i = 1; i <= n; ++i) rep[i] = i;
  }
  int find(int x) { return (rep[x] == x) ? x : rep[x] = find(rep[x]); }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x != y) size[x] += size[y], rep[y] = x, nbe[x] += nbe[y];
  }
};
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  write(fixed, setprecision(12));
  int n, m;
  read(n, m);
  vector<pair<int, pair<int, int>>> in;
  for (int i = 1; i <= m; ++i) {
    int a, b, cost;
    read(a, b, cost);
    in.push_back({cost, {a, b}});
  }
  sort(in.begin(), in.end());
  reverse(in.begin(), in.end());
  int ans = 0;
  DSU dsu(n);
  for (auto el : in) {
    int a = el.second.first;
    int b = el.second.second;
    int cost = el.first;
    int rep_a = dsu.find(a);
    int rep_b = dsu.find(b);
    if (rep_a == rep_b && dsu.size[rep_a] >= 1 + dsu.nbe[rep_a]) {
      ans += cost;
      ++dsu.nbe[rep_a];
    }
    if (rep_a != rep_b && dsu.size[rep_a] + dsu.size[rep_b] >=
                              1 + dsu.nbe[rep_a] + dsu.nbe[rep_b]) {
      ans += cost;
      dsu.unite(rep_a, rep_b);
      ++dsu.nbe[dsu.find(rep_a)];
    }
  }
  write(ans, "\n");
}
