Time limit
2s
Memory limit
128 MB
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 = 11Prod(2322) = 2 * 3 * 2 * 2 = 24Prod3(2322) = 3 * 2 * 2 = 12Prod3(47) = Prod(47) = 4 * 7 = 28Smallest(427) = 2First(427) = 412034 @ 217 = 5 * (4 * 3 * 2) + 1 * (2 + 1 + 7) + 1 = 131A valid expression can be formed only by the following rules.
X is a valid expression.A and B are valid expressions, then A @ B is also a valid expression.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.
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.
If a valid expression with value G can be formed, print the minimum number of @ operators in such an expression. Otherwise, print -1.