Code Lock
Uncle Scrooge's treasure vault, filled with gold, now features a new, uniquely designed combination lock. This lock appears as a circular ring with 8 identical drums, each numbered from 0 to 9. These numbers are evenly spaced around the circle, and each drum follows a sequence where the number after (i) (where (0 i 8)) is (i+1), and the number after 9 is 0.
Only the drum currently positioned at the top of the ring can be rotated. The entire ring can also be rotated, either clockwise or counterclockwise, to bring a different drum to the top. The lock will open only when a specific sequence of numbers is displayed on the drums.
In one second, Uncle Scrooge can either rotate the top drum by one position in either direction (advancing to the next or previous number on this drum) or rotate the entire ring by one drum position clockwise or counterclockwise to change which drum is at the top. Uncle Scrooge, eager to access his gold swiftly, wants to open the lock using the fewest possible operations. Your task is to determine the minimum number of operations required to achieve this.
Input
The input consists of two sets of 8 digits each, read from standard input. Within each set, digits are presented consecutively without separators, and a space separates the two sets. The first set of digits represents the initial configuration of the drums, starting from the top. The second set indicates the configuration that will unlock the vault, also starting from the top.
Output
The output should be a single number, printed to standard output, representing the minimum number of operations needed to open the lock. If it is impossible to transform the initial configuration into the unlocking configuration, output the number (-1).