#include <bits/stdc++.h>
using namespace std;
const int inf = 1.01e9;
const double eps = 1e-9;
const int N = 2e5 + 10;
int head[N], sdflkj[N], to[N], col[N], ec = 1;
inline void add(int x, int y, int c) {
  ec++;
  to[ec] = y;
  sdflkj[ec] = head[x];
  head[x] = ec;
  col[ec] = c;
}
int oncactus[N];
int mark[N];
int st[N], stc = 0;
vector<vector<int> > cc;
void dfs(int x, int pr = -1) {
  mark[x] = 1;
  for (int e = head[x]; e; e = sdflkj[e])
    if ((e ^ pr) != 1) {
      int y = to[e];
      if (mark[y] == 1) {
        vector<int> v;
        v.push_back(col[e]);
        oncactus[e >> 1] = 1;
        for (int i = stc - 1;; i--) {
          assert(i >= 0);
          v.push_back(col[st[i]]);
          oncactus[st[i] >> 1] = 1;
          if (to[st[i] ^ 1] == y) break;
        }
        cc.push_back(v);
      } else if (mark[y] == 0) {
        st[stc++] = e;
        dfs(y, e);
        stc--;
      }
    }
  mark[x] = 2;
}
int safe[N];
int dead[N];
int cnt[N];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; (i) < (m); ++i) {
    int x, y, c;
    scanf("%d%d%d", &x, &y, &c);
    --x, --y;
    add(x, y, c);
    add(y, x, c);
  }
  dfs(0);
  for (int i = 1; i <= ec / 2; ++i)
    if (!oncactus[i]) {
      safe[col[i * 2]] = 1;
    }
  vector<vector<int> > cc2;
  for (int i = 0; (i) < (((int)(cc).size())); ++i) {
    int issafe = 0;
    sort((cc[i]).begin(), cc[i].end());
    for (int j = 0; (j) < (((int)(cc[i]).size())); ++j) {
      int c = cc[i][j];
      if (safe[c]) issafe = 1;
      if (j + 1 < ((int)(cc[i]).size()) && cc[i][j] == cc[i][j + 1]) issafe = 1;
    }
    if (!issafe)
      cc2.push_back(cc[i]);
    else {
      for (int j = 0; (j) < (((int)(cc[i]).size())); ++j) cnt[cc[i][j]] += 5;
    }
  }
  cc = cc2;
  for (int i = 0; (i) < (((int)(cc).size())); ++i) {
    for (int j = 0; (j) < (((int)(cc[i]).size())); ++j) cnt[cc[i][j]]++;
  }
  int ANS = 0;
  while (1) {
    int ch = 0;
    cc2.clear();
    for (int i = 0; (i) < (((int)(cc).size())); ++i) {
      vector<int> v;
      for (int j = 0; (j) < (((int)(cc[i]).size())); ++j) {
        int x = cc[i][j];
        if (cnt[x] > 1) v.push_back(x);
      }
      if (((int)(v).size()) == 0) {
        ANS--;
        ch = 1;
      } else if (((int)(v).size()) == 1) {
        int x = v[0];
        cnt[x]--;
        ch = 1;
      } else {
        cc2.push_back(cc[i]);
      }
    }
    cc = cc2;
    if (!ch) break;
  }
  set<int> second;
  for (int i = 1; i <= ec / 2; ++i) second.insert(col[2 * i]);
  ANS += ((int)(second).size());
  printf("%d\n", ANS);
  return 0;
}
