#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long a[1010], b[1010], c[1010];
struct UF {
  vector<long long> par;
  vector<long long> sz;
  UF() {}
  UF(long long n) {
    par.resize(n);
    sz.resize(n, 1);
    for (long long i = 0; i < n; i++) par[i] = i;
  }
  long long find(long long x) {
    if (x == par[x]) return x;
    return par[x] = find(par[x]);
  }
  void unite(long long x, long long y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (sz[x] < sz[y]) swap(x, y);
    sz[x] += sz[y];
    par[y] = x;
  }
  bool same(long long x, long long y) { return find(x) == find(y); }
};
signed main() {
  cin >> n >> m;
  vector<pair<long long, long long> > d;
  for (long long i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    a[i]--;
    b[i]--;
    d.push_back(pair<long long, long long>(c[i], i));
  }
  sort(d.begin(), d.end());
  UF uf(n);
  long long ans = 0;
  for (long long i = 0; i < m; i++) {
    uf.unite(a[d[i].second], b[d[i].second]);
    bool f = true;
    for (long long j = 1; j < n; j++) {
      if (uf.same(0, j) == false) f = false;
    }
    if (f) {
      ans = m - i - 1;
      break;
    }
  }
  cout << ans << endl;
}
