#include <bits/stdc++.h>
using namespace std;
struct edg {
  int to, c, id;
};
int n, m;
vector<edg> adj[10001];
pair<int, int> edgs[50001];
vector<int> by_color[50001];
struct dsu {
  vector<int> par;
  vector<int> sz;
  vector<bool> ok;
  vector<set<int>> colors;
  dsu(int n) : par(n, -1), ok(n, false), colors(n, set<int>()) {
    sz.assign(n, 1);
  }
  int get(int x) {
    if (par[x] == -1) return x;
    return par[x] = get(par[x]);
  }
  void merge(int x, int y) {
    int px = get(x), py = get(y);
    if (px == py) {
      ok[px] = true;
      return;
    }
    if (sz[px] > sz[py]) {
      swap(px, py);
      swap(x, y);
    }
    for (auto i : colors[px]) colors[py].insert(i);
    colors[px].clear();
    ok[py] = ok[py] | ok[px];
    sz[py] += sz[px];
    par[px] = py;
  }
};
dsu d(10001);
int dft, dfn[10001], low[10001], paredg[10001], paredgc[10001], act[10001],
    par[10001];
int comp[50001], cid = 1;
void dfs(int x) {
  dfn[x] = low[x] = ++dft;
  act[x] = 1;
  for (auto i : adj[x]) {
    if (!dfn[i.to]) {
      paredg[i.to] = i.id;
      paredgc[i.to] = i.c;
      par[i.to] = x;
      dfs(i.to);
      low[x] = min(low[x], low[i.to]);
      if (low[i.to] == dfn[i.to]) {
        comp[i.id] = cid++;
        d.colors[cid - 1].insert(i.c);
        d.ok[cid - 1] = 1;
      }
    } else if (paredg[x] != i.id) {
      low[x] = min(low[x], dfn[i.to]);
      if (act[i.to]) {
        comp[i.id] = cid;
        d.colors[cid].insert(i.c);
        int akt = x;
        while (akt != i.to) {
          comp[paredg[akt]] = cid;
          d.colors[cid].insert(paredgc[akt]);
          akt = par[akt];
        }
        cid++;
      }
    }
  }
  act[x] = 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int& a = edgs[i].first;
    int& b = edgs[i].second;
    int c;
    cin >> a >> b >> c;
    adj[a].push_back({b, c, i});
    adj[b].push_back({a, c, i});
    by_color[c].push_back(i);
  }
  dfs(1);
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j < (int)by_color[i].size(); ++j) {
      d.merge(comp[by_color[i][j - 1]], comp[by_color[i][j]]);
    }
  }
  int ans = 0;
  for (int i = 1; i < cid; ++i) {
    cerr << d.colors[i].size() << " " << d.ok[i] << "\n";
    if (d.par[i] == -1) {
      ans += (int)d.colors[i].size() - (!d.ok[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}
