Digit Operations to Make Two Integers Equal
You are given two integers n and m that consist of the same number of digits.
You can perform the following operations any number of times:
- Choose any digit from
nthat is not9and increase it by1. - Choose any digit from
nthat is not0and decrease it by1.
The integer n must not be a prime number at any point, including its original value and after each operation.
The cost of a transformation is the sum of all values that n takes throughout the operations performed.
Return the minimum cost to transform n into m. If it is impossible, return -1.
Example 1
Input
n = 10, m = 12Output
85One valid minimum-cost sequence is 10 -> 20 -> 21 -> 22 -> 12, whose summed values are 10 + 20 + 21 + 22 + 12 = 85.
Example 2
Input
n = 4, m = 8Output
-1It is impossible to make
n equal to m.Constraints
- 1 <= n, m < 10^4
- n and m consist of the same number of digits.