const
  MaxN = 20020;
  MaxM = 200020;


var
   inFile, outFile: Text;
   n, m, time, root, sol, a, b, c : longint;
   dfsNum, currSon, city, p, deg : array[0..MaxN] of longint;
   a1, b1, c1, adj, asfalt : array[0..2*MaxM] of longint;
   mark : array[0..MaxN] of boolean;



procedure Input;
var
   i, a, b, c: longint;

begin

   assign( inFile, 'asfalt.in' );
   reset( inFile );
   fillchar( deg, sizeof( deg ), 0 );

   readln( inFile, n, m );
   for i := 1 to m do begin
      readln( inFile, a, b, c );
      deg[ a ] := deg[ a ] + 1;
      deg[ b ] := deg[ b ] + 1;
      a1[ i ] := a; b1[ i ] := b; c1[ i ] := c;
      a1[ i + m ] := b; b1[ i + m ] := a; c1[ i + m] := c;
   end;

   close( inFile );

end;



procedure Output;
var
  i : longint;

begin

   assign( outFile, 'asfalt.out' );
   rewrite( outFile );
   writeln( outFile, sol );
   for i := 1 to sol - 1 do
      write( outFile, city[ i ], ' ' );
   writeln( outFile, city[ sol ] );
   close( outFile);

end;



(* simulacija liste preko counting sorta *)
procedure ListSimulation;
var
   i: longint;

begin

   p[0] := 0;
   for i := 1 to n do
      p[ i ] := p[ i - 1 ] + deg[ i - 1 ];

   for i := 1 to 2 * m do begin
      adj[ p[ a1[ i ] ] ] := b1[ i ];
      asfalt[ p[ a1[ i ] ] ] := c1[ i ];
      p[ a1[ i ] ] := p[ a1[ i ] ] + 1;
   end;

   for i := 1 to n do
      p[ i ] := p[ i - 1 ] + deg[ i - 1 ];

end;



procedure DFS( u : longint );
var
   v, i : longint;

begin

   time := time + 1;
   dfsNum[ u ] := time;

   for i := p[ u ] to p[ u ] + deg[ u ] - 1 do begin
      v := adj[ i ];


      (* ako je u-v tree-edge i cvor v nije obidjen *)
      if ( (asfalt[ i ] = 1) AND (dfsNum[ v ] = 0) ) then begin
         currSon[ u ] := v;
         DFS( V );
      end;

      (* ako u-v nije tree-edge i cvor v je vec obidjen *)
      if ( (asfalt[ i ] = 0) AND (dfsNum[ v ] <> 0) ) then begin

         (* tada je u-v back ili "cross" edge i markiramo krajeve *)
	 mark[ u ] := true;
	 mark[ v ] := true;

	 (* ako je u-v back edge, proverimo da li nam treba novi root *)
	 if ( (currSon[ v ] <> 0) AND (dfsNum[ currSon[ v ] ] > dfsNum[ root ]) ) then
	    root := currSon[ v ];

      end;

   end;

   currSon[ u ] := 0;

end;



procedure DFS2( u : longint );
var
   v, i : longint;

begin

   mark[ u ] := true;
   sol := sol + 1;
   city[ sol ] := u;

   for i := p[ u ] to p[ u ] + deg[ u ] - 1 do begin
      v := adj[ i ];
      if ( (asfalt[ i ] = 1) AND (NOT mark[ v ]) ) then
         DFS2(v);
   end;

end;



begin

   Input;
   ListSimulation;

   fillchar( dfsNum, sizeof( dfsNum ), 0 );
   fillchar( currSon, sizeof( currSon ), 0);
   fillchar( mark, sizeof( mark ), false );
   root := 1;  time := 0;  sol := 0;

   DFS(1);
   DFS2(root);

   Output;

end.
