#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5 + 9, maxV = 5e5, MOD = 1e9 + 7, AND = (1 << 20) - 1,
          lg = 20;
int n, m;
pair<pair<int, int>, int> edg[maxN];
int comp[maxN];
long long ans = 0;
int find(int u) {
  if (u == comp[u]) return u;
  return comp[u] = find(comp[u]);
}
void unite(int u, int v) { comp[find(u)] = find(v); }
vector<pair<int, int> > adj[maxN];
int dp[lg][maxN], he[maxN], maxis[lg][maxN];
void dfs(int nd, int an, int lev) {
  dp[0][nd] = an;
  he[nd] = lev;
  for (pair<int, int> sn : adj[nd])
    if (sn.second != an)
      dfs(sn.second, nd, lev + 1), maxis[0][sn.second] = sn.first;
}
int getma(int u, int v) {
  if (he[u] > he[v]) swap(u, v);
  int ret = 0;
  for (int i = lg - 1; i >= 0; i--) {
    if (dp[i][v] != -1 && he[dp[i][v]] >= he[u]) {
      ret = max(ret, maxis[i][v]);
      v = dp[i][v];
    }
  }
  if (u == v) return ret;
  for (int i = lg - 1; i >= 0; i--) {
    if (dp[i][u] != dp[i][v]) {
      ret = max(ret, maxis[i][v]);
      ret = max(ret, maxis[i][u]);
      v = dp[i][v];
      u = dp[i][u];
    }
  }
  ret = max(ret, maxis[0][v]);
  ret = max(ret, maxis[0][u]);
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> edg[i].second >> edg[i].first.second >> edg[i].first.first,
        edg[i].second--, edg[i].first.second--;
  sort(edg, edg + m);
  for (int i = 0; i < n; i++) comp[i] = i;
  vector<pair<pair<int, int>, int> > desc;
  for (int i = 0; i < m; i++) {
    int one = find(edg[i].first.second), two = find(edg[i].second);
    if (one != two) {
      unite(one, two);
      adj[edg[i].first.second].push_back(
          pair<int, int>(edg[i].first.first, edg[i].second));
      adj[edg[i].second].push_back(
          pair<int, int>(edg[i].first.first, edg[i].first.second));
    } else {
      desc.push_back(edg[i]);
    }
  }
  dfs(0, -1, 0);
  for (int i = 1; i < lg; i++) {
    for (int j = 0; j < n; j++) {
      if (dp[i - 1][j] == -1)
        dp[i][j] = -1;
      else
        dp[i][j] = dp[i - 1][dp[i - 1][j]],
        maxis[i][j] = max(maxis[i - 1][j], maxis[i - 1][dp[i - 1][j]]);
    }
  }
  for (pair<pair<int, int>, int> el : desc)
    ans += max(0, getma(el.first.second, el.second) + 1 - el.first.first);
  cout << ans << '\n';
}
