var
ship,father:array[1..50002] of longint;
fb,fc,sb,sc,b,c,a,i,n,k,sum:longint;
procedure init;
var
i:longint;
begin
readln(n,k);
for i:=1 to n do
begin
ship[i]:=0;
father[i]:=i;
end;
sum:=0;
end;
function getancestor(x:longint):longint;
var
temp:longint;
begin
temp:=x;
while (father[temp]<>temp) do temp:=father[temp];
getancestor:=temp;
end;
function getship(x:longint):longint;
begin
if father[x]=x then
begin
getship:=0;
exit;
end;
ship[x]:=(ship[x]+getship(father[x])) mod 3;
father[x]:=getancestor(x);
getship:=ship[x];
end;
procedure work;
var
i:longint;
begin
for i:=1 to k do
begin
readln(a,b,c);
if (b>n) or (c>n) or ((a=2) and (b=c)) then
begin
inc(sum);
continue;
end;
fb:=getancestor(b);
fc:=getancestor(c);
sc:=getship(c);
sb:=getship(b);
if a=1 then
begin
if fc<>fb then
begin
father[fc]:=b;
ship[fc]:=(3-sc) mod 3;
end
else if sb<>sc then inc(sum);
end
else
begin
if fc<>fb then
begin
father[fc]:=b;
ship[fc]:=(2-sc) mod 3;
end
else if sb<>(sc+1) mod 3 then inc(sum);
end;
end;
end;
begin
init;
work;
writeln(sum);
end.
/**************************************************************
Problem: 2238
User: admin
Language: Pascal
Result: Wrong Answer
****************************************************************/