cho.sh
Notes
Loading...

New Operator

Time limit

2s

Memory limit

128 MB

Problem

For a positive integer N, define these functions.

  • Sum(N) is the sum of all digits of N.
  • Prod(N) is the product of all digits of N.
  • Prod3(N) is the product of the three largest digits of N. If N has fewer than three digits, then Prod3(N) = Prod(N).
  • Smallest(N) is the smallest digit of N.
  • First(N) is the first digit of N.

For two values X and Y, define the operator @ as follows.

X @ Y = 5 * Prod3(X) + First(X) * Sum(Y) + Smallest(Y)

The following identities hold.

  • Sum(47) = 4 + 7 = 11
  • Prod(2322) = 2 * 3 * 2 * 2 = 24
  • Prod3(2322) = 3 * 2 * 2 = 12
  • Prod3(47) = Prod(47) = 4 * 7 = 28
  • Smallest(427) = 2
  • First(427) = 4
  • 12034 @ 217 = 5 * (4 * 3 * 2) + 1 * (2 + 1 + 7) + 1 = 131

A valid expression can be formed only by the following rules.

  1. The input value X is a valid expression.
  2. If A and B are valid expressions, then A @ B is also a valid expression.
  3. Any expression that cannot be formed by the rules above is not valid.

Given X and a target value G, find the minimum number of @ operators in a valid expression whose value is G. If no such expression exists, print -1.

Input

The first line contains X and G. X is a positive integer not greater than 1,000,000, and G is a positive integer not greater than 2,000,000,000.

Output

If a valid expression with value G can be formed, print the minimum number of @ operators in such an expression. Otherwise, print -1.