A number lock has N dials. Each dial carries the digits 0 through 9 in order.
Turning a dial up changes the visible digit from 0 to 1, from 1 to 2, and so on, with 9 changing back to 0. Turning a dial down moves in the opposite direction.
You can turn several dials at once. The dials turned together must be consecutive, and there is no limit on how many. Every dial turned together moves one step in the same direction.
For example, if the lock reads 123, you can turn all three dials down to get 012, turn all three up to get 234, turn only the middle dial up to get 133, or turn the first two dials down to get 013. You cannot reach 224 with a single turn.
Given the current state S and the target state T, write a program that finds the minimum number of turns needed to change S into T.