#include<bits/stdc++.h>
using namespace std;
const int N=1e4*5+1;
int n,k,a,x,y,fa[N],ans=0,t1,t2,e[N];
int find(int o)
{
if(fa[o]!=o) fa[o]=find(fa[o]);
return fa[o];
}
int main()
{
memset(e,0,sizeof(e));
cin>>n>>k;
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=k;i++)
{
cin>>a>>x>>y;
if(x>n||y>n) ans++;
else if(a==1)
{
t1=find(x);t2=find(y);
if( (e[t1]==t2) || (e[t2]==t1) ) ans++;
else if(t1!=t2) fa[t1]=t2;
}
else if(a==2)
{
t1=find(x);t2=find(y);
if(t1==t2){ans++;continue;}
if(e[t2]==t1){ans++;continue;}
e[t1]=t2;
}
}
cout<<ans;
return 0;
}