Zrakoplović the penguin wants to learn how to fly!
The space in which he will learn to fly can be imagined as a cube of dimensions , divided into unit cubes. Each unit cube can be described with three coordinates , where , and are integers between and . The coordinate denotes the distance from the left edge of the space, the coordinate denotes the distance from the front edge of the space, and the coordinate denotes the height.
Some of these unit cubes contain clouds, and some do not.
Zrakoplović is afraid of clouds, so he will learn to fly only where there are no clouds. He initially finds himself at a position , such that (i.e. at height ), and wants to get to position .
At the moment, he is perfecting the skill of flying in directions that are parallel to one of the axes of space (i.e. in the direction of the x-axis, y-axis or z-axis), and in one wing flap he can cross at most one unit cube.
Before he decides to fly, Zrakoplović wants to know how many wing flaps he needs to get to the desired position. While he is preparing for the flight, help him answer that question.
Input Specification
The first line contains an integer , the size of the dimensions of the space in which Zrakoplović learns to fly.
The second line contains three integers , and , the starting position of Zrakoplović.
The third line contains three integers , and , the ending position of Zrakoplović.
This is followed by binary matrices of dimensions that describe the space, where the -th matrix describes the space at height . The upper-left corner has the coordinates . All elements in the same row have the same -coordinate and all elements in the same column have the same -coordinate.
0
denotes a unit cube in which there are no clouds, and 1
denotes a unit cube in which there are
clouds.
The start and end position of Zrakoplović will not be a cloud.
Output Specification
In the first and only line, print the smallest number of wing flaps that Zrakoplović must make to reach
the desired position. If Zrakoplović cannot reach the desired position, print -1
.
Constraints
Subtask | Points | Constraints |
---|---|---|
1 | 7 | |
2 | 16 | There are no clouds. |
3 | 22 | All positions with -coordinate greater than 1 will be clouds. |
4 | 25 | No additional constraints. |
Sample Input 1
2
1 1 1
1 1 2
00
10
01
00
Sample Output 1
1
Explanation for Sample 1
Zrakoplović can reach the desired position in one wing flap by moving in the direction of the -axis for one unit cube.
Sample Input 2
3
2 3 1
1 1 1
000
010
000
111
111
111
111
111
111
Sample Output 2
3
Explanation for Sample 2
Zrakoplović can reach the desired position in three wing flaps by first moving to position , then to and finally to .
Sample Input 3
3
2 1 1
3 2 2
000
010
110
010
001
001
101
110
000
Sample Output 3
3
Comments