#include <cstdio>
#include <algorithm>
#include <cstdlib>
using namespace std;
int father[1000000];
int value[1000000];
int find(int x){
if(x == father[x]) return x;
int fx = find(father[x]);
value[x] = value[x] + value[father[x]];
return father[x] = fx;
}
bool set_union(int a, int b, int s){
int aa = find(a);
int bb = find(b);
if(aa == bb){
if((value[a] - value[b]) != s)
return true;
}
father[aa] = bb;
value[aa] = s + value[b] - value[a];
return false;
}
int main()
{
//freopen("ztest.txt","r",stdin);
int n, m;
while(scanf("%d%d", &n, &m) == 2){
int ans = 0;
for(int i = 0; i <= n; i++)
{
father[i] = i;
value[i] = 0;
}
while(m--){
int a, b, s;
scanf("%d%d%d", &a, &b, &s);
if(set_union(a-1, b, s))
ans++;
}
printf("%d\n",ans);
}
return 0;
}