require "big/big_int" starting=1 base=gets.not_nil!.to_i while (s=gets)!=nil n=BigInt.new(s.not_nil!,base: base)+(starting-2) digits=1 expbase=BigInt.new("1") while (x=digits*expbase*(base-1))<=n digits+=1 expbase*=base n-=x end num=expbase+n/digits d=digits-1-n%digits d.times{num/=base} puts num%base end