#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>

using namespace std;

int n;
int m;
int cnt[20005];

vector<int> e1[20005];
vector<int> e2[20005];

vector<int> e[20005];
bool mark[20005];
int lvl[20005];

void go(int node, int par) {
    lvl[node] = par == -1 ? -1 : (lvl[par] + 1);
    if (par != -1) {
        e[par].push_back(node);
        for (int i = 0; i < e1[node].size(); ++i) {
            int nnode = e1[node][i];
            if (nnode == par) continue;
            e[par].push_back(nnode);
        }
        for (int i = 0; i < e2[node].size(); ++i) {
            int nnode = e2[node][i];
            if (nnode == par) continue;
            e[par].push_back(nnode);
        }
    }
    for (int i = 0; i < e1[node].size(); ++i)
        if (e1[node][i] != par)
            go(e1[node][i], node);
}

bool dfs(int node, int par) {
    mark[node] = true;
    for (int i = 0; i < e[node].size(); ++i) {
        int nnode = e[node][i];
        if (mark[nnode] && lvl[nnode] < lvl[node]) return true;
        if (!mark[nnode] && dfs(nnode, node)) return true;
    }
    return false;
}

bool can(int node) {
    go(node, -1);
    memset(mark, 0, sizeof(mark));
    if (dfs(node, -1)) return false;
    return true;
}

int main() {
    freopen("asfalt.in", "r", stdin);
    freopen("asfalt.out", "w", stdout);
    memset(cnt, 0, sizeof(cnt));
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i) {
        e1[i].clear();
        e2[i].clear();
    }
    for (int i = 0; i < m; ++i) {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        if (c == 0) {
            ++cnt[a];
            ++cnt[b];
            e2[a - 1].push_back(b - 1);
            e2[b - 1].push_back(a - 1);
        } else {
            e1[a - 1].push_back(b - 1);
            e1[b - 1].push_back(a - 1);
        }
    }
    if (n > 2000) {
        int res = 0;
        for (int i = 1; i <= n; ++i)
            if (!cnt[i])
                ++res;
        printf("%d\n", res);
        for (int i = 1; i <= n; ++i)
            if (!cnt[i])
                printf("%d ", i);
        printf("\n");
    } else {
        vector<int> res;
        res.clear();
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j)
                e[j].clear();
            if (can(i))
                res.push_back(i + 1);
        }
        printf("%d\n", res.size());
        for (int i = 0; i < res.size(); ++i)
            printf("%d ", res[i]);
        printf("\n");
    }
    return 0;
}
