#include <bits/stdc++.h>
#define rand() dis(gen)
using namespace std;
const int N = 1005;
mt19937 gen(time(0));
uniform_int_distribution<int> dis(0, 1e6);
int i, n, m, x[N], y[N], z[N], val[N], nx[N], ny[N], rs;
int main() {
ifstream cin("party.in");
ofstream cout("party.out");
ios_base::sync_with_stdio(0);
cin >> n >> m;
for(i = 1; i <= m; ++i) {
cin >> x[i] >> y[i] >> z[i];
if(z[i] == 1) ny[y[i]] = 1;
if(z[i] == 2) nx[x[i]] = 1;
if(z[i] == 3) nx[x[i]] = ny[y[i]] = 1;
}
for(i = 1; i <= m; ++i)
if(!(val[x[i]] ^ nx[x[i]]) && !(val[y[i]] ^ ny[y[i]])) {
if(rand() & 1) val[x[i]] ^= 1;
else val[y[i]] ^= 1;
i = 0;
}
for(i = 1; i <= n; ++i) if(val[i]) ++rs;
cout << rs << '\n';
for(i = 1; i <= n; ++i) if(val[i]) cout << i << '\n';
return 0;
}