#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  register int c = getchar();
  x = 0;
  int neg = 0;
  for (; ((c < 48 || c > 57) && c != '-'); c = getchar())
    ;
  if (c == '-') {
    neg = 1;
    c = getchar();
  }
  for (; c > 47 && c < 58; c = getchar()) {
    x = (x << 1) + (x << 3) + c - 48;
  }
  if (neg) x = -x;
}
inline void writeln(int x) {
  char buffor[21];
  register int i = 0;
  int neg = 0;
  if (x < 0) {
    neg = 1;
    x = -x;
  }
  do {
    buffor[i++] = (x % 10) + '0';
    x /= 10;
  } while (x);
  i--;
  if (neg) putchar('-');
  while (i >= 0) putchar(buffor[i--]);
  putchar('\n');
}
const int N = (int)2e5 + 10;
int n, lab[N], m, sumE[N];
pair<int, pair<int, int> > e[N];
void input() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++)
    cin >> e[i].second.first >> e[i].second.second >> e[i].first;
}
int findset(int u) {
  if (lab[u] < 0)
    return u;
  else
    return lab[u] = findset(lab[u]);
}
void Union(int u, int v) {
  if (lab[u] < lab[v]) {
    lab[u] += lab[v];
    lab[v] = u;
    sumE[u] += sumE[v] + 1;
  } else {
    lab[v] += lab[u];
    lab[u] = v;
    sumE[v] += sumE[u] + 1;
  };
}
void solve() {
  sort(e + 1, e + 1 + m, greater<pair<int, pair<int, int> > >());
  for (int i = 1; i <= n; i++) lab[i] = -1;
  for (int i = 1; i <= n; i++) sumE[i] = 0;
  int res = 0;
  for (int i = 1; i <= m; i++) {
    int u = e[i].second.first, v = e[i].second.second;
    u = findset(u);
    v = findset(v);
    if (u == v) {
      if (sumE[u] < abs(lab[u])) {
        sumE[u]++;
        res += e[i].first;
      };
    } else {
      if (sumE[u] + sumE[v] == abs(lab[u]) + abs(lab[v])) continue;
      Union(u, v);
      res += e[i].first;
    }
  }
  cout << res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  input();
  solve();
  return 0;
}
