N, B = input().split()
# res = 0
# for n in N:
#     if ord(n)>=ord('0') and ord(n)<=ord('9'):
#         res = res*int(B)+ord(n)-ord('0')
#     else:
#         res = res*int(B)+ord(n)-ord('A')+10
# print(res)
print(int(N,int(B)))