program prosramoc;
var k,m,i:longint; j:qword;
function jz(x:longint):longint;
var i,n:longint; s:string; y:qword;
a:array[1..255]of byte;
begin
n:=0; y:=x; s:='';
repeat
inc(n);
a[n]:=y mod k;
y:=y div k;
until y=0;
for i:=n downto 1 do s:=s+chr(a[i]+ord('0'));
val(s,y,i); exit(y);
end;
begin
assign(input,'sramoc.in'); reset(input);
assign(output,'sramoc.out'); rewrite(output);
readln(k,m);
i:=0;
repeat
inc(i);
j:=jz(i);
until (j mod m = 0);
writeln(j);
close(input); close(output);
end.