The Value of an Expression

No attempts yetTime limit1sMemory limit128 MB

Problem

The operation $#$ is defined on any two positive integers as follows.

For positive integers $x$ and $y$, $$(x # y) = (\text{sum of the digits of } x) \times (\text{greatest digit of } y) + (\text{least digit of } y).$$

For example, $(9 # 30) = 9 \times 3 + 0 = 27$, whereas $(30 # 9) = 3 \times 9 + 9 = 36$.

An expression (in this problem) is one of the following:

  • the single variable a, whose value is a positive integer, or
  • something of the form (expression # expression).

For example, the following are all valid expressions:

  • a
  • (a#a)
  • ((a#a)#a)
  • (a#((a#a)#((a#a)#a)))

The value of an expression is determined by the value of a together with the operation $#$: the expression consisting of just a has value $a$, and (E1 # E2) has the value obtained by applying $#$ to the values of its two subexpressions.

Given the value of $a$, determine the least number of $#$ operations needed to build an expression whose value equals $K$ (a positive integer).

Input

Two positive integers are given: the value of the integer variable $a$ ($1 \le a \le 999999999$) and the target expression value $K$ ($1 \le K \le 999999999$).

Output

Print the least number of $#$ operations required. If it is impossible to obtain $K$ as the value of an expression for the given $a$, print NEVAR instead.