AdvancedRating 1600
Open the Lock
A lock has 4 wheels, each 0–9, starting at 0000. One move turns one wheel one step up or down (9 and 0 are neighbours). Some combinations are dead ends you must never pass through. Print the fewest moves to reach the target, or -1.
Input
The dead-end count, then the dead ends on one line (or an empty line), then the target.
Output
One integer.
Sample 1
Input
5 0201 0101 0102 1212 2002 0202
Output
6
Sample 2
Input
1 8888 0009
Output
1
Output appears here