The Value of an Expression

Time limit1sMemory limit128 MB

Summary
Given a starting value a, use the operation x#y = (digit sum of x)*(max digit of y) + (min digit of y) and find the fewest operations to reach K, or report NEVAR.
Level

Medium7 of 10

Topics
BFS, Math, Implementation, Greedy
Solved
No attempts yet

Problem

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

For positive integers xx and yy, (x#y)=(sum of the digits of x)×(greatest digit of y)+(least digit of 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×3+0=27(9 \# 30) = 9 \times 3 + 0 = 27, whereas (30#9)=3×9+9=36(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 aa, and (E1 # E2) has the value obtained by applying #\# to the values of its two subexpressions.

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

Input

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

Output

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

Examples2

  1. Example 1

    Input
    718 81
    
    Expected output
    3
    
  2. Example 2

    Input
    999 333
    
    Expected output
    NEVAR