357. Remote Control

Time limit per test: 0.25 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard



A couple of years ago Peter Berlandin bought a brand new “Berlony” TV set. The TV set supports 100 channels, numbered from 0 to 99. The remote control of the TV set has 13 buttons:
1 2 3
4 5 6
7 8 9
-- 0
When pressing the button "↑" the number of the current channel increases by 1 (if the current channel is the 99th, it switches to 0). If you press the button "↓" the number of the current channel decreases by 1 (if the number of the current channel is 0, it switches to the 99th). Switching to the channel from 0 to 9 can be done by pressing the button at the remote control with the corresponding number. Switching to the channel from 10 to 99 can be done by pressing the "--" button followed by two buttons in accordance with the number of the channel required. Casual handling and time don't do good to any of us! Finally the remote control was damaged and went out of order. Now some of the buttons are not functioning. Your task is to switch from X channel to Y using the minimum number of clicks.

Input
The first 4 lines contain information about the productivity of every button at the remote control. 0 stands for the broken button and 1 stands for the one that is functioning. The first line describes "1", "2", "3" and "↑" buttons accordingly. The second line describes "4", "5", "6" and "↓" buttons accordingly. The third line describes "7", "8" and "9" buttons accordingly. The fourth line describes "--" and "0" buttons accordingly. The fifth line describes X and Y.

Output
Print the answer to the problem — the minimum number of clicks to switch from X to Y — in the output file. Print -1 if it is impossible to switch from X to Y.

Example(s)
sample input
sample output
1 1 1 1
1 1 1 1
1 1 1
1 1
23 52
3

sample input
sample output
0 0 1 1
1 1 1 1
1 1 1
1 1
23 52
4




Online Contester Team © 2002 - 2010. All rights reserved.