#45378: 濾掉錯答


x__1 (^_^ 809-34)


b,a=1,0
p,n=map(int,input().split())
while n:
 if n&1:
  a+=b
 n>>=1
 b*=p
print(a%(1<<32))