#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int fa[N];
int find(int x)
{
if(x != fa[x])
fa[x] = find(fa[x]);
return fa[x];
}
int get(int x, int y)
{
int r1 = find(fa[x]);
int r2 = find(fa[y]);
fa[r1] = r2;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, k;
cin >> n >> k;
for(int i = 1; i <= 3 * n; i++)
{
fa[i] = i;
}
int cnt = 0;
for(int i = 1; i <= k; i++)
{
int x, y, z;
cin >> z >> x >> y;
if(x > n || y > n)
cnt++;
else
{
if(z == 1)
{
if(find(x + n) == find(y) || find(x + 2 * n) == find(y))
cnt++;
else
{
get(x, y);
get(x + n, y + n);
get(x + 2 * n, y + 2 * n);
}
}
else
{
if(x == y || find(x) == find(y) || find(x + 2 * n) == find(y))
cnt++;
else
{
get(x, y + 2 * n);
get(x + n, y);
get(x + 2 * n, y + n);
}
}
}
}
cout << cnt << endl;
return 0;
}