Sequence#8500

Sequence

1 secondo
128 megabyte
Facile

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,

prb8500.gif

where s_i = or s_i = , k_i are integers and should be minimized.

Input

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).

Output

Print one integer number .

Esempi

Esempio 1

Loading
Loading