#include<iostream>
using namespace std;
const int max_n = 1e5 + 5;
int pre[max_n], re[max_n], num_ct, say_ct, ans = 0;
int root(int x)
{
int temp = pre[x];
if (x ^ pre[x]) {
pre[x] = root(pre[x]);
re[x] = (re[x] + re[temp]) % 3;
}
return pre[x];
}
int main()
{
cin >> num_ct >> say_ct;
for (int i = 0; i < num_ct; i++)pre[i] = i, re[i] = 0;
for (int i = 0; i < say_ct; i++) {
int rela, x, y;
cin >> rela >> x >> y;
if (x > num_ct || y > num_ct || (rela == 2 && x == y)) {
ans++;
continue;
}
int a = root(x), b = root(y);
if (rela == 1) {
if (a == b && re[x] ^ re[y]) {
ans++;
continue;
}
else if (a ^ b) {
pre[a] = b;
re[a] = (re[y] - re[x] + 3) % 3;
}
}
if (rela == 2) {
if (a == b && ((re[x] - re[y] + 3) % 3) ^ 1) {
ans++;
continue;
}
if (a ^ b) {
pre[a] = b;
re[a] = (re[x] - re[y] + 1 + 3) % 3;
}
}
}
cout << ans << endl;
return 0;
}