program sequence;

{$APPTYPE CONSOLE}

uses
  SysUtils;

Var
  inf,outf:text;
  p,cif:Byte;
  n,k,st,l,n_ch,raz,ch10:Int64;
  i:byte;
begin
  Assign(inf,'input.txt');
  Reset(inf);
  Readln(inf,p,n);

  k:=1; st:=1;
  While n>0 Do Begin
    l:=k*st*(p-1);
    Dec(n,l);
    Inc(k); st:=st*p;
  End;
  Dec(k); Inc(n,l); st:=st div p;
  n_ch:=(n+k-1) div k;
  raz:=n mod k;
  If raz=0 Then Inc(raz,k);

  If k=1 Then ch10:=n_ch
         Else ch10:=st+(n_ch-1);
  For i:=1 To k-raz Do
    ch10:=ch10 div p;
  cif:=ch10 mod p;
  Assign(outf,'output.txt');
  Rewrite(outf);
  If cif<10 Then Writeln(outf,cif)
  Else Writeln(outf,Chr(cif-10+Ord('A')));
  Close(outf);
End.

