#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
FILE * FIN,* FOUT;
int n,m,i,a,b,c,i1,t,asd[20001],nizic[20001],k,nizz[20001];
vector <int> ffs,niz[20001],stek;
vector <int>::iterator it;
int put(int &s){
    for(i1=1;i1<=n;++i1) nizic[i1]=0;
    for(i1=1;i1<=n;++i1) nizz[i1]=0;
    if(asd[s]==1) return(0);
    for(it=niz[s].begin();it<niz[s].end();++it){
        stek.push_back(*(it));
        nizz[*it]=s;}

    do{
       k=stek.back();

       stek.pop_back();
       for(it=niz[k].begin();it<niz[k].end();++it){
                                                   if(nizz[k]!=*it){
                                                    if(nizic[*it]==1) return(0);
                                                   nizic[*it]=1;
                                                   nizz[*it]=k;
                                                   stek.push_back(*it);}}
       }while(stek.empty()!=true);
       ffs.push_back(s);
     return(1);}

main(){
    FIN=fopen("asfalt.in","r");
    FOUT=fopen("asfalt.out","w");
       fscanf(FIN,"%d %d",&n,&m);
       for(i=0;i<m;++i){
                    fscanf(FIN,"%d %d %d",&a,&b,&c);
                    if(c==0){
                             asd[a]=1;
                             asd[b]=1;}
                    else{
                         niz[a].push_back(b);
                         niz[b].push_back(a);}
                                              }
        t=0;
        for(i=1;i<=n;++i) t+=put(i);
        fprintf(FOUT,"%d\n",t);
        for(it=ffs.begin();it<ffs.end();++it) fprintf(FOUT,"%d ",*it);
        fclose(FIN);
        fclose(FOUT);}
