#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define maxn 50005
int n,m,father[maxn*3],ans=0,d;
int find(int x){
if(father[x]==x) return x;
else return father[x]=find(father[x]);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<=n*3+5;i++) father[i]=i;
int x,y,x1,x2,x3,y1,y2,y3;
while(m--){
scanf("%d%d%d",&d,&x,&y);
if(x>n||y>n){ ans++;continue; }
x1=find(x);x2=find(x+n);x3=find(x+2*n);// 2*n 天敌 n食物
y1=find(y);y2=find(y+n);y3=find(y+2*n);
if(d==1){
if(x1==y2||y1==x2) ans++;
else {
father[x1]=y1;
father[x2]=y2;
father[x3]=y3;//x和y是同类
}
}
else {
if(x1==y1||x1==y2) ans++;
else{
father[y1]=x2;// x的食物和y是一类
father[y2]=x3;// y 和x的食物是同一类
father[y3]=x1;// x和y的天敌是同一类
}
}
}
printf("%d\n",ans);
return 0;
}