#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=+;
int p[N];
int w[N];
int find(int x)
{
if(p[x]==x)
return x;
int root=find(p[x]);
w[x]+=w[p[x]];
p[x]=root;
return p[x];
}
int n,m;
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<=n;i++)
p[i]=i,w[i]=;
int ans=;
for(int i=;i<=m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
int pa=find(a);
int pb=find(b);
if(pa==pb)
{
if(w[a]+c!=w[b])
ans++;
}
else
{
p[pb]=pa;
w[pb]=w[a]-w[b]+c;
}
}
printf("%d\n",ans);
}
return ;
}