//1481
#include <iostream>
#include <cstdio>
using namespace std;
#define N 50010
int parent[N],sum;
void init()
{
for(int i=1;i<=N;i++)parent[i]=i;
}
int find(int x)
{
if(x==parent[x])return x;
else return find(parent[x]);
}
void marge(int a,int b)
{
int ta = find(a);
int tb = find(b);
if(ta!=tb) {parent[ta]=tb;sum--;}
}
int main()
{
int n,m,i,x,y,k;
while(scanf("%d%d",&n,&m)!=EOF&&(n+m))
{
init(); sum=n;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&k,&x,&y);
if(x>n || y> n)
{sum--;continue;}
if(k==1) marge(x,y);
else if(k=2&&x==y)sum--;
}
printf("%d
",n-sum);
}
return 0;
}
#include <iostream>
#include <cstdio>
using namespace std;
#define N 50010
int parent[N],sum;
void init()
{
for(int i=1;i<=N;i++)parent[i]=i;
}
int find(int x)
{
if(x==parent[x])return x;
else return parent[x]=find(parent[x]);
}
void marge(int a,int b)
{
int ta = find(a);
int tb = find(b);
if(ta!=tb) {parent[ta]=tb;sum--;}
}
int main()
{
int n,m,i,x,y,k;
while(scanf("%d%d",&n,&m)!=EOF&&(n+m))
{
init(); sum=n;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&k,&x,&y);
if(x>n || y> n)
{sum--;continue;}
if(k==1) marge(x,y);
else if(k==2 && x==y )sum--;
}
printf("%d
",n-sum);
}
return 0;
}