eolymp
bolt
Try our new interface for solving problems
Problems

Magic machine

Magic machine

Ibrahim has a black magic machine. It has three buttons and a display. Display can show no more than a four-digit number. Each of the buttons changes the number in some way: the first is multiplies it by 3, the second adds to it the sum of digits, and the third button subtracts 2. If the number becomes negative or greater than 9999, the machine breaks.

Ibrahim can press the buttons in any order. He wondered how to get on the display the number b after a certain sequence of clicks, if currently the machine shows a. Help him to find the minimum number of keystrokes.

Input

Two integers a and b (1a, b9999).

Output

Print the minimal number of operations to get b from a.

Time limit 1 second
Memory limit 128 MiB
Input example #1
14 15
Output example #1
2
Input example #2
18 12
Output example #2
3
Input example #3
14 29
Output example #3
2