#include <bits/stdc++.h>
using namespace std;
int n, m, S, T, tot_ = 0, tot = 1, top = 0, cyc = 0;
int head[40100], head_[40100], q[40100], d[40100], cur[40100], fa[40100],
    depth[40100], v[40100];
bool vis[40100], inc[40100];
struct edge {
  int to, next, f;
} e[200100];
struct edge_ {
  int to, next, val;
} e_[200100];
void addedge_(int x, int y, int col) {
  e_[++tot_].to = y;
  e_[tot_].val = col;
  e_[tot_].next = head_[x];
  head_[x] = tot_;
  e_[++tot_].to = x;
  e_[tot_].val = col;
  e_[tot_].next = head_[y];
  head_[y] = tot_;
}
void addedge(int u, int v, int f) {
  e[++tot].to = v;
  e[tot].next = head[u];
  e[tot].f = f;
  head[u] = tot;
  e[++tot].to = u;
  e[tot].next = head[v];
  e[tot].f = 0;
  head[v] = tot;
}
bool bfs() {
  for (int i = S; i <= T; i++) d[i] = 0;
  int f = 0, r = 1;
  q[1] = S;
  d[S] = 1;
  while (f < r) {
    int u = q[++f];
    for (int i = head[u]; i; i = e[i].next)
      if (e[i].f > 0 && !d[e[i].to]) {
        d[e[i].to] = d[u] + 1;
        q[++r] = e[i].to;
      }
  }
  return d[T];
}
int dfs(int u, int flow) {
  if (u == T) return flow;
  int rest = flow;
  for (int i; cur[u]; cur[u] = e[i].next) {
    i = cur[u];
    int v = e[i].to;
    if (e[i].f > 0 && d[v] == d[u] + 1) {
      int t = dfs(v, min(e[i].f, rest));
      rest -= t;
      e[i].f -= t;
      e[i ^ 1].f += t;
      if (!rest) return flow;
    }
  }
  if (rest == flow) d[u] = 0;
  return flow - rest;
}
int dinic() {
  int ans = 0;
  while (bfs()) {
    for (int i = S; i <= T; i++) cur[i] = head[i];
    ans += dfs(S, 1000000000);
  }
  return ans;
}
void dfs(int x, int pre, int col) {
  fa[x] = pre, q[++top] = x;
  vis[x] = true;
  v[x] = col;
  depth[x] = depth[pre] + 1;
  for (int i = head_[x]; i; i = e_[i].next) {
    if (e_[i].to == pre) continue;
    if (!vis[e_[i].to])
      dfs(e_[i].to, x, e_[i].val);
    else if (depth[e_[i].to] < depth[x]) {
      int sum = 0;
      cyc++;
      int j = top;
      while (q[j] != e_[i].to) {
        addedge(cyc, v[q[j]], 1);
        sum++;
        inc[q[j]] = true;
        j--;
      }
      addedge(S, cyc, sum);
      addedge(cyc, e_[i].val, 1);
    }
  }
  top--;
  if (!inc[x] && fa[x]) {
    addedge(S, ++cyc, 1);
    addedge(cyc, v[x], 1);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  tot = 1;
  S = 0;
  T = n + m + 1;
  for (int x, y, col, i = 1; i <= m; i++) {
    scanf("%d%d%d", &x, &y, &col);
    addedge_(x, y, col + n);
  }
  dfs(1, 0, 0);
  for (int i = n + 1; i <= n + m; i++) addedge(i, T, 1);
  printf("%d\n", dinic());
  return 0;
}
