#!/usr/bin/python2 # -*- coding: utf-8 -*- # † from math import log def rep(B, i): return (B**i - 1) / (B-1) def g(B, i): if i == 1: return 1 if i == 2: return B return (i-2) * B**(i-1) + rep(B, i-1) * (B-2) + 2 def f(B, D): lo, hi = 0, 10**6 while hi - lo > 1: md = (lo + hi) / 2 if g(B, md) <= D: lo = md else: hi = md pos = g(B, lo) dif = D - pos z, r = divmod(dif, lo) w = z + B**(lo-1) m = int(log(w, B) + 1e-9) + 1 return (w / B**(m-1-r)) % B B = int(raw_input()) D = int(raw_input(), base=B) res = f(B, D) print res