#include<stdio.h>
const int maxn = 200005;
int f[maxn], val[maxn];//val记录下标所能到达最左端的值
int Find(int x)
{
int k = f[x];
if(f[x] != x)
{
f[x] = Find(f[x]);
val[x] += val[k];
}
return f[x];
}
int main()
{
int N, M;
while(scanf("%d%d", &N, &M) != EOF)
{
int i, u, v, w, ans=0;
for(i=0; i<=N; i++)
{
f[i] = i;
val[i] = 0;
}
while(M--)
{
scanf("%d%d%d", &u, &v, &w);
u = u-1;//注意为什么要减一
int ru = Find(u), rv = Find(v);
if(ru == rv && val[u]+w != val[v])
ans++;
else if(ru < rv)
{
f[rv] = ru;
val[rv] = val[u] - val[v] + w;
}
else if(ru > rv)
{
f[ru] = rv;
val[ru] = val[v] - val[u] - w;
}
}
printf("%d\n", ans);
}
return 0;
}