#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int M = 50005;
struct node
{
int p;
int relation;
}s[M];
int n, m;
int Find(int x)
{
if(x == s[x].p)
return x;
int t = s[x].p;
s[x].p = Find(t);
s[x].relation = (s[x].relation + s[t].relation) % 3; //路径压缩,推理得到。
return s[x].p;
}
int main()
{
int a, b, d;
int x, y;
int ans;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) {
s[i].p = i;
s[i].relation = 0;
}
ans = 0;
for(int i = 1; i <= m; i++) {
scanf("%d%d%d", &d, &a, &b);
if(a > n || b > n) {
ans++;
continue;
}
if(d == 2 && a == b) {
ans++;
continue;
}
x = Find(a);
y = Find(b);
if(x != y)
{
s[y].p = x;
s[y].relation = (3 + d - 1 + s[a].relation - s[b].relation) %3; //由向量的到此公式。
}
else
{
if(d == 1 && s[a].relation != s[b].relation) {
ans++;
continue;
}
if(d == 2 &&(3 + s[b].relation - s[a].relation) % 3 != d - 1) { //由向量公式,
// a --> 根,== s[b].relation 根--> b == s[a].relation
ans++;
continue;
}
}
}
printf("%d\n", ans);
return 0;
}