Lame King Junior
Consider an infinite chessboard that extends indefinitely in all directions, composed of square cells. We will designate one specific cell as the center. The vertical line passing through this center cell is labeled as 0. The vertical lines to the right of the center are numbered sequentially as 1, 2, 3, and so on, while those to the left are numbered -1, -2, -3, etc. Similarly, horizontal lines are numbered, with positive numbers above the center and negative numbers below. Thus, any cell on the board can be identified by a pair of numbers representing its vertical and horizontal line numbers.
Now, imagine a king positioned at the center of the board, in the cell with coordinates (0, 0). The king moves according to standard chess rules, meaning it can move to any adjacent cell horizontally, vertically, or diagonally. However, certain directions may be restricted.
Your task is to determine the minimum number of moves the king needs to reach a cell located at coordinates (x, y).
Input
The first line of the input contains 8 numbers, each indicating whether movement in a specific direction is allowed. A 1 means the direction is permitted, while a 0 means it is not. The directions are specified in a counterclockwise order, starting from the positive horizontal direction (i.e., right, right-up, up, left-up, left, left-down, down, right-down). The second line provides the coordinates x and y of the target cell (-500 ≤ x, y ≤ 500).
Output
Output a single integer representing the minimum number of allowed moves the king needs to make to travel from the cell (0, 0) to the cell (x, y). If it is impossible for the king to reach the target cell, output -1.