#include <bits/stdc++.h>
using namespace std;
const long long MX = 10011;
const long long MX_color = 20011;
struct FlowEdge {
  long long v, u;
  long long cap, flow = 0;
  FlowEdge(long long v, long long u, long long cap) : v(v), u(u), cap(cap) {}
};
struct Dinic {
  const long long flow_INF = 1e18;
  vector<FlowEdge> edges;
  vector<vector<long long> > adj;
  long long n, m = 0, s, t;
  vector<long long> level, ptr;
  queue<long long> q;
  Dinic(long long n, long long s, long long t) : n(n), s(s), t(t) {
    adj.resize(n);
    level.resize(n);
    ptr.resize(n);
  }
  void add_edge(long long v, long long u, long long cap) {
    edges.emplace_back(v, u, cap);
    edges.emplace_back(u, v, 0);
    adj[v].push_back(m);
    adj[u].push_back(m + 1);
    m += 2;
  }
  bool bfs() {
    while (!q.empty()) {
      long long v = q.front();
      q.pop();
      for (long long id : adj[v]) {
        if (edges[id].cap - edges[id].flow < 1) continue;
        if (level[edges[id].u] != -1) continue;
        level[edges[id].u] = level[v] + 1;
        q.push(edges[id].u);
      }
    }
    return level[t] != -1;
  }
  long long dfs(long long v, long long pushed) {
    if (pushed == 0) return 0;
    if (v == t) return pushed;
    for (long long& cid = ptr[v]; cid < (long long)adj[v].size(); cid++) {
      long long id = adj[v][cid];
      long long u = edges[id].u;
      if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
        continue;
      long long tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
      if (tr == 0) continue;
      edges[id].flow += tr;
      edges[id ^ 1].flow -= tr;
      return tr;
    }
    return 0;
  }
  long long flow() {
    long long f = 0;
    while (true) {
      fill(level.begin(), level.end(), -1);
      level[s] = 0;
      q.push(s);
      if (!bfs()) break;
      fill(ptr.begin(), ptr.end(), 0);
      while (long long pushed = dfs(s, flow_INF)) {
        f += pushed;
      }
    }
    return f;
  }
};
bool vis[MX];
long long dist[MX];
pair<long long, long long> parent[MX];
vector<pair<long long, long long> > adj[MX];
multiset<pair<pair<long long, long long>, long long> > ari;
vector<vector<long long> > ciclos;
long long queda[MX_color];
void quita(long long u, long long v, long long c) {
  queda[c]--;
  ciclos.push_back({});
  long long sz = ciclos.size();
  ciclos[sz - 1].push_back(c);
  while (u != v) {
    long long c1 = parent[u].second;
    long long c2 = parent[v].second;
    if (dist[u] > dist[v]) {
      queda[c1]--;
      u = parent[u].first;
      ciclos[sz - 1].push_back(c1);
    } else if (dist[u] < dist[v]) {
      queda[c2]--;
      v = parent[v].first;
      ciclos[sz - 1].push_back(c2);
    } else {
      queda[c1]--;
      queda[c2]--;
      v = parent[v].first;
      u = parent[u].first;
      ciclos[sz - 1].push_back(c1);
      ciclos[sz - 1].push_back(c2);
    }
  }
  return;
}
void dfs(long long u, long long d) {
  dist[u] = d;
  vis[u] = true;
  for (auto par : adj[u]) {
    long long v = par.first;
    long long c = par.second;
    auto it = ari.find({{min(u, v), max(u, v)}, c});
    if (it != ari.end()) {
      ari.erase(it);
      if (!vis[v]) {
        parent[v] = {u, c};
        dfs(v, d + 1);
      } else {
        quita(u, v, c);
      }
    }
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout.precision(10);
  cout << fixed;
  set<long long> colores_tot;
  long long n, m, x, y, c;
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) {
    cin >> x >> y >> c;
    adj[x].push_back({y, c});
    adj[y].push_back({x, c});
    ari.insert({{min(x, y), max(x, y)}, c});
    queda[c]++;
    colores_tot.insert(c);
  }
  for (long long i = 1; i <= n; i++) {
    if (!vis[i]) dfs(i, 0);
  }
  long long nn = MX_color + ciclos.size() + 2;
  long long s = 0;
  long long t = nn - 1;
  Dinic G(nn, s, t);
  for (long long i = 0; i < ciclos.size(); i++) {
    long long sz = ciclos[i].size();
    G.add_edge(s, MX_color + i, sz - 1);
    for (long long j = 0; j < sz; j++) {
      G.add_edge(MX_color + i, ciclos[i][j], 1);
    }
  }
  for (long long i = 1; i < MX_color; i++) {
    if (queda[i] != 0) {
      G.add_edge(s, i, queda[i]);
    }
  }
  for (long long x : colores_tot) {
    G.add_edge(x, t, 1);
  }
  cout << G.flow() << "\n";
  return 0;
}
