/*
自己太水了,参考大神才写出来的。
路径压缩。。没接触过
题意应该是,输入a,b,c;b在a的后面c处,虽然题目是300的环,可它在同一列就可以,所以可以看成无限长的直线,只要注意已确定位置的两个人再次输出的情况
*/
#include <stdio.h>
#include <string.h>
int f[50005],d[50005];//d[i]为i在其根结点后多远
int n,m,t;
int a,b,c;
int find(int x)
{
if(x==f[x])
return f[x];
int temp=f[x];
f[x]=find(f[x]);
d[x]+=d[temp]; //f[4]=3,f[3]=1,f[1]=1;d[4]=d[4]+d[3]+d[1];
return f[x];
}
void judge(int aa,int bb) // 1 2 150,d[2]=150;3 4 200,d[4]=200;2 4 250,d[3]=200
{ // 1--150--2--50--3--200--4;d[3]=250-d[4]+d[2];
f[bb]=aa;
d[bb]=c-d[b]+d[a];
}
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
int i,j;
t=0;
for(i=1;i<=n;i++)
f[i]=i,d[i]=0;
while(m--)
{
scanf("%d %d %d",&a,&b,&c);
int aa,bb;
aa=find(a);
bb=find(b);
if(aa!=bb)
judge(aa,bb);
else
{
if(d[b]-d[a]!=c)
t++;
}
}
printf("%d\n",t);
}
return 0;
}