import numpy def Base_10_to_n(X, n, R): if X == n ** (R + 1): return 1 X = X // (n ** R) return X % n def Base_n_to_10(X,n): out = 0 tmp = 1 for i in range(1,len(str(X))+1): out += int(X[-i])*tmp tmp *= n return out#int out def f(l, B): return ((l * B - l - 1) * B ** l + 1) // (B - 1) B = input() N = input() B = int(B) N = Base_n_to_10(N, B) - 1 l = 0 lv = 0 rv = 100000 while rv - lv > 1: m = (lv + rv) // 2 if f(m, B) < N: lv = m else: rv = m l = lv bound = f(l, B) N -= bound Q = N // (l + 1) + B ** l R = N % (l + 1) # print(N, Q, R, l, bound) print(Base_10_to_n(Q, B, l - R)) # print(Q, R)