#include <bits/stdc++.h>
const int maxn = 50010;
const int maxm = 100010;
using namespace std;
bool vis[10010], used[maxm];
int cur[maxn], opt[maxn], dis[maxn], d[maxn];
int n, m, e, ter[maxm], nxt[maxm], lnk[maxn], w[maxm], rec[maxm], cnt, s, t;
struct epoch {
  int x, y;
} ed[40010];
void Add(int x, int y, int z, int r) {
  ter[++e] = y, nxt[e] = lnk[x], lnk[x] = e, w[e] = z, rec[e] = e + 1;
  ter[++e] = x, nxt[e] = lnk[y], lnk[y] = e, w[e] = r, rec[e] = e - 1;
}
void add(int x, int y) { ++e, ed[e].x = x, ed[e].y = y; }
void dfs(int p, int las, int &ret, int &id) {
  ret = id = 0;
  int tmp, tmpid;
  for (int i = lnk[p]; i; i = nxt[i])
    if (used[i]) {
      used[i] = used[rec[i]] = false;
      if (!vis[ter[i]]) {
        ++cnt, add(m + cnt, w[i]);
        ret = ter[i], id = cnt;
        continue;
      }
      vis[ter[i]] = false;
      dfs(ter[i], p, tmp, tmpid);
      if (tmp)
        add(m + tmpid, w[i]), add(s, m + tmpid);
      else
        add(s, w[i]);
      if (tmp && tmp != p) ret = tmp, id = tmpid;
    }
}
bool bfs() {
  int he = 0, ta = 1;
  opt[1] = s;
  for (int i = s; i <= t; ++i) dis[i] = 0;
  dis[s] = 1;
  while (he != ta) {
    ++he;
    int u = opt[he];
    for (int i = lnk[u]; i; i = nxt[i])
      if (!dis[ter[i]] && w[i]) dis[ter[i]] = dis[u] + 1, opt[++ta] = ter[i];
  }
  return (dis[t]);
}
int dfs(int p, int sum) {
  int res = 0;
  if (p == t) return sum;
  for (int i = cur[p]; i; i = nxt[i])
    if (w[i] && dis[ter[i]] == dis[p] + 1) {
      int tmp = dfs(ter[i], min(sum - res, w[i]));
      w[i] -= tmp, w[rec[i]] += tmp, res += tmp;
      if (w[i]) cur[p] = i;
      if (res == sum) return res;
    }
  return res;
}
int dinic() {
  int ret = 0;
  while (bfs()) {
    for (int i = s; i <= t; ++i) cur[i] = lnk[i];
    ret += dfs(s, m);
  }
  return ret;
}
bool cmp(epoch a, epoch b) { return (a.x < b.x || (a.x == b.x && a.y < b.y)); }
int main() {
  scanf("%d%d", &n, &m);
  int x, y, z;
  for (int i = 1; i <= m; ++i)
    scanf("%d%d%d", &x, &y, &z), Add(x, y, z, z), ++d[x], ++d[y];
  memset(vis, true, sizeof(vis));
  cnt = 0;
  memset(used, true, sizeof(used));
  int st = 1;
  for (int i = 1; i <= n; ++i)
    if (d[i] != 2) st = i;
  s = cnt = e = 0, vis[st] = false, dfs(st, 0, x, y), t = m + cnt + 1;
  memset(lnk, 0, sizeof(lnk)), memset(nxt, 0, sizeof(nxt));
  sort(ed + 1, ed + e + 1, cmp);
  int tot = e;
  e = 0;
  for (int i = 1, j; i <= tot; i = j) {
    for (j = i + 1; j <= tot && ed[i].x == ed[j].x && ed[i].y == ed[j].y; ++j)
      ;
    Add(ed[i].x, ed[i].y, j - i, 0);
  }
  for (int i = 1; i <= m; ++i) Add(i, t, 1, 0);
  printf("%d\n", dinic());
  return 0;
}
