type mat=array[1..2000,1..2000]of byte;
var matr,a:mat;
res,j,z,m,x,y,n,i:longint;
dobro,t:boolean;
br:array[1..20000]of longint;
o:array[1..20000]of boolean;
ulaz,izlaz:text;
procedure dfs(c:longint);
var i:longint;
begin
o[c]:=true;
for i:=1 to n do
 if matr[c,i]=1 then
  if o[i]then begin
              dobro:=false;
              exit
              end
         else begin
              matr[c,i]:=0;
              matr[i,c]:=0;
              if dobro=false then exit;
              dfs(i);
              if dobro=false then exit
              end;
end;
begin
assign(ulaz,'asfalt.in');
reset(ulaz);
assign(izlaz,'asfalt.out');
rewrite(izlaz);

readln(ulaz,n,m);
if n<=2000 then begin
for i:=1 to m do begin
                 read(ulaz,x,y,z);
                 if z=1 then begin
                             a[x,y]:=1;
                             a[y,x]:=1
                             end
                        else begin
                             a[x,y]:=2;
                             a[y,x]:=2
                             end;
                 end;
for i:=1 to n do begin
                 t:=true;
                 for j:=1 to n do
                  if a[i,j]=2 then begin
                                   t:=false;
                                   break
                                   end;
                 if t then begin
                           for j:=1 to n do
                            for z:=1 to n do matr[j,z]:=a[j,z];
                           for j:=1 to n do o[j]:=false;
                           dobro:=true;
                           dfs(i);
                           if dobro then begin
                                         inc(res);
                                         br[res]:=i
                                         end;
                           end
                 end;
writeln(izlaz,res);
for i:=1 to res-1 do write(izlaz,br[i],' ');write(izlaz,br[res])
                end
                else begin
                     for i:=1 to m do begin
                                      read(ulaz,x,y,z);
                                      if z=0 then begin
                                                  o[x]:=true;
                                                  o[y]:=true
                                                  end
                                      end;
                     for i:=1 to n do
                      if o[i]=false then begin
                                         inc(res);
                                         br[res]:=i
                                         end;
                     writeln(izlaz,res);
                     for i:=1 to res-1 do write(izlaz,br[i],' ');write(izlaz,br[res])
                     end;
close(ulaz);
close(izlaz);
end.