Consider positive integers and . Your task is to represent as the algebraic sum of integer powers of with the minimal possible number of terms. In other words,

where s_i = or s_i = , k_i are integers and should be minimized.
The first line contains positive integer written without leading zeroes. contains no more than digits. Second line contains integer (1 \le B \le 10^6).
Print one integer number .
Esempio 1