program Odometers; uses crt; var infile,outfile:text; x,y:integer; R1:string; d1, d2: char; d1counter, d2counter: integer; R1int: longint; kmcounter: longint; begin assign(infile,'DATA11.txt'); reset(infile); assign(outfile,'OUT11.txt'); rewrite(outfile); for x:=1 to 5 do begin readln(infile,R1); readln(infile,d1); readln(infile,d2); d1counter := 0; for y:= 1 to 6 do if R1[y]=d1 then inc(d1counter); val(R1,R1int); kmcounter := 0; repeat inc(kmcounter); inc(R1int); if R1int = 1000000 then R1int := 0; str(R1int,R1); while length(R1)<6 do R1:='0'+R1; d2counter := 0; for y:= 1 to 6 do if R1[y]=d2 then inc(d2counter); until d2counter = d1counter; writeln(outfile,R1,' ',kmcounter); end; close(infile); close(outfile); end.