#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
vvi vabc;
bool func(vi v0) {
  vvi vcand(1, v0);
  while (!vcand.empty()) {
    vi v1 = vcand.back();
    vcand.pop_back();
    for (int(i)(0); (i) < (vabc.size()); ++(i)) {
      if (vabc[i][2] < v1[2] && (vabc[i][0] == v1[0] || vabc[i][1] == v1[0])) {
        int ai = vabc[i][0] == v1[0] ? 0 : 1;
        int bi = ai ? 0 : 1;
        if (vabc[i][bi] == v1[1]) {
          return true;
        }
        vcand.push_back({vabc[i][bi], v1[1], v1[2] - vabc[i][2]});
      }
    }
    sort((vcand).begin(), (vcand).end(),
         [](const vi &lhs, const vi &rhs) { return lhs[2] > rhs[2]; });
  }
  return false;
}
int main() {
  int N, M;
  cin >> N >> M;
  for (int(i)(0); (i) < (M); ++(i)) {
    int a, b, c;
    cin >> a >> b >> c;
    vabc.push_back({a, b, c});
  }
  int ans(0);
  for (int(i)(0); (i) < (vabc.size()); ++(i)) {
    if (func(vabc[i])) {
      vabc.erase(vabc.begin() + i);
      ++ans;
    }
  }
  cout << ans << endl;
  return 0;
}
