{$o-,q+,r+}
{$apptype console}
uses sysutils;
var a:array [1..1000] of integer;
    ac, cy, i, b:integer;
    s:string;
begin
  ac:=0;
  assert (not seekeoln); read (b); assert (seekeoln); readln;
  assert ((b>=2) and (b<=9));
  readln (s); assert (seekeof); assert (length (s) in [1..101]);
  for i:=1 to length (s) do assert (s[i] in ['0'..'9']);
  if length (s)>1 then assert (s[1]<>'0');
  if length (s)=101 then begin
    assert (s[1]='1'); for i:=2 to 101 do assert (s[i]='0');
  end;
  for i:=1 to length (s) do dec (s[i], 48);
  repeat
    cy:=0; for i:=1 to length (s) do inc (cy, ord (s[i]));
    if cy=0 then break;
    cy:=0;
    for i:=1 to length (s) do begin
      cy:=cy*10+ord (s[i]);
      s[i]:=chr (cy div b);
      cy:=cy mod b;
    end;
    if cy=0 then begin
      for i:=length (s) downto 1 do if s[i]=#0 then s[i]:=#9 else begin
        dec (s[i]); break
      end;
      cy:=b;
    end;
    inc (ac); a[ac]:=cy;
  until false;
  for i:=ac downto 1 do write (a[i]); writeln;
end.