#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <queue>
#include <stack>
#include <string.h>
using namespace std;


int n, m;
vector <int> adj[200005];
int compnum, comp[200005];
int f[200005];
bool mark[200005], compok[200005];
bool l[200005];
int cap[200005];

void input(){
    FILE *f = fopen("asfalt.in", "r");
    fscanf(f, "%d%d", &n, &m);
    for (int i=1; i<=n; i++) cap[i]=mark[i]=l[i]=compok[i]=0;
    for (int i=0; i<m; i++){
        int a, b, c;
        fscanf(f, "%d%d%d", &a, &b, &c);
        if (c==0){
            l[a]=l[b]=1;
        } else{
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
    }
    fclose(f);
}

void dfs(int u){
    mark[u]=true;
    comp[u]=compnum;
    for (int i=0; i<adj[u].size(); i++){
        int v=adj[u][i];
        if (!mark[v]){
            mark[v]=true; f[v]=u;
            dfs(v);
        } else{
            if (v!=f[u]) compok[compnum]=true;
        }
    }
}

void solve(){
    for (int i=1; i<=n; i++)
        if (!mark[i]) {
            f[i]=-1;
            dfs(i);
            compnum++;
        }
}

void output(){
    FILE *f = fopen("asfalt.out", "w");
    vector<int> sol;
    for (int i=1; i<=n; i++)
        if (!l[i] && !compok[comp[i]]) sol.push_back(i);
    fprintf(f, "%d\n", sol.size());
    for (int i=0; i<sol.size(); i++){
        if (i>0) fprintf(f, " ");
        fprintf(f, "%d", sol[i]);
    }
    fprintf(f, "\n");
    fclose(f);
}


int main(){
    input();
    solve();
    output();
    return 0;
}
