#include <bits/stdc++.h>
const long long INF = LLONG_MAX / 2;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
using namespace std;
long long int ans = 0, sum = 0, n, k, counter = 0, w, h;
string s;
vector<long long int> v;
vector<pair<long long int, long long int>> vp;
vector<vector<long long int>> vv(110, vector<long long int>(50, 0));
vector<tuple<long long int, long long int, long long int>> vt;
map<long long int, long long int> mp;
vector<long long int> par;
long long int parent(long long int num) {
  if (par[num] == num)
    return num;
  else {
    long long int res = parent(par[num]);
    par[num] = res;
    return res;
  }
}
long long int same(long long int v01, long long int v02) {
  if (parent(v01) == parent(v02))
    return 1;
  else
    return 0;
}
void unionn(long long int v01, long long int v02) {
  par[parent(v02)] = parent(v01);
}
long long int judge() {
  long long int flg = 1;
  for (int i = (long long int)((0)); i < (long long int)(n - 1); i++) {
    if (!same(i, i + 1)) flg = 0;
  }
  return flg;
}
int main() {
  cin >> n >> k;
  par.resize(n);
  for (int i = (long long int)((0)); i < (long long int)(n); i++) par[i] = i;
  for (int i = (long long int)((0)); i < (long long int)(k); i++) {
    long long int a, b, c;
    cin >> a >> b >> c;
    vt.emplace_back(c, a, b);
  }
  sort(vt.begin(), vt.end());
  while (true) {
    long long int a, b, c;
    tie(c, a, b) = vt[0];
    if (same(a - 1, b - 1))
      ans++;
    else
      unionn(a - 1, b - 1);
    vt.erase(vt.begin());
    if (judge()) break;
  }
  ans += vt.size();
  cout << ans << endl;
}
