A cargo terminal keeps a diagnostic freight lift parked at floor reading x, while a permanently fixed reference lift used for calibration sits at floor reading y. Using the lift's maintenance panel, you may repeat, any number of times, exactly one of the following actions on the diagnostic lift until its reading equals y:
Each action, of whichever kind, counts as one step, regardless of how far it moves the reading. Report the minimum number of steps needed to bring the diagnostic lift's reading to exactly y.
A single line containing two integers x and y, separated by a space.
A single integer: the minimum number of steps required to make the diagnostic lift's reading equal y.
Example 1
Input
25 1
Expected
2
Explanation
25 is a multiple of 5, so dividing gives 5 in one step; 5 is again a multiple of 5, so dividing gives 1 in a second step. Two steps suffice, and no single step (a plain increment or decrement) can do better since |25-1| = 24 far exceeds 2.
Example 2
Input
54 2
Expected
4
Explanation
Raising 54 by 1 gives 55, a multiple of 5 (step 1). Dividing 55 by 5 gives 11, a multiple of 11 (step 2). Dividing 11 by 11 gives 1 (step 3). Raising 1 by 1 gives 2 (step 4). Four steps total; moving directly from 54 down to 2 would take 52 decrements, far worse, and no shorter combination of shortcuts reaches 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →