#include<stdio.h>

#include<string.h>

#define N  200100

struct node {

int x,count;

}pre[N];

int find(int n) {

if(n!=pre[n].x) {

int h=pre[n].x;

pre[n].x=find(pre[n].x);

pre[n].count=pre[n].count+pre[h].count;

}

return pre[n].x;

}

int Union(int x,int y,int d) {

      int a=find(x);

 int b=find(y);

 if(a==b) {

 if(pre[x].count-pre[y].count==d)

 return 1;

 return 0;

 }

 else {

 pre[a].x=b;

 pre[a].count=pre[y].count-pre[x].count+d;

 return 1;

 }

}

int main() {

int n,m,a,b,c,count,sum,flag,i;

while(scanf("%d%d",&n,&m)!=EOF) {

for(i=0;i<=n;i++) {

pre[i].x=i;

pre[i].count=0;

}

count=0;

while(m--) {

scanf("%d%d%d",&a,&b,&c);

a--;

if(Union(a,b,c)==0)

count++;

}

printf("%d\n",count);

}

return 0;

}

05-28 20:30