{
ZADATAK: balegovi
JEZIK: pascal
}
var n,i,poc,kraj:longint;
    a,b,inde:array[1..1000000] of longint;
    ui:array[1..1000000] of boolean;
    f:text;
begin
 assign(f,'balegovi.in');
 reset(f);
 readln(f,n);
 for i:=1 to n do
   readln(f,a[i],b[i]);
 close(f);
 inde[1]:=1;
 ui[1]:=true;
 kraj:=1;
 while not ui[a[inde[kraj]]] do
    begin
     inde[kraj+1]:=a[inde[kraj]];
     inc(kraj);
     ui[inde[kraj]]:=true;
    end;
 poc:=1;
 while inde[poc]<>a[inde[kraj]] do
    inc(poc);
 assign(f,'balegovi.out');
 rewrite(f);
 writeln(f,kraj-poc+1);
 for i:=poc to kraj do
   write(f,b[inde[i]],' ');
 close(f);
end.
