#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long longinf = 1LL << 60;
const int mx = 100010;
const long long mod = 1e9 + 7;
int par[mx];
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
void unite(int a, int b) {
  int x = find(a);
  int y = find(b);
  par[x] = y;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i <= n; i++) {
    par[i] = i;
  }
  vector<pair<int, pair<int, int>>> e;
  for (int i = (int)(0); i < (int)(m); ++i) {
    int s, t, c;
    cin >> s >> t >> c;
    s--;
    t--;
    e.push_back({c, {s, t}});
  }
  sort(e.begin(), e.end());
  int cnt = 1;
  for (int i = 0; i < e.size(); i++) {
    int a = e[i].second.first;
    int b = e[i].second.second;
    if (find(a) == find(b)) {
      continue;
    }
    unite(a, b);
    cnt++;
    if (cnt == n) {
      cout << e.size() - i - 1 << endl;
      break;
    }
  }
  return 0;
}
