Every rabbit knows what to do in case of a flood. All you need to do is to get to a point above the water level and wait for Mazai. But when the actual flood happens, rabbits start to panic. That’s why we ask for your help to create an optimal evacuation plan for rabbits.
Let’s describe the flood as a game between a rabbit and water. Game takes place in a rectangular grid with n rows and m columns. Let’s say that a point in a row i and a column j has coordinates (i, j). It is known, that for all 1 ≤ i ≤ n, 1 ≤ j ≤ m a cell with coordinates (i, j) has height hij. The rabbit starts at a point with coordinates (r1, c1), water starts at point with coordinates (r2, c2). Moreover, the rabbit has a property named jump height.
The rabbit and water take turns, the rabbit makes the first move. After each move the rabbit either doesn’t move or jumps to any adjacent cell. Cells are adjacent, if they have a common side. In addition to this, the rabbit cannot move to a cell, which height exceeds the height of a current cell more than on jump height. Water just fills all the cells that have adjacent cell filled with water of greater or equal height. Both water and the rabbit should stay within the game grid.
Rabbit can only survive in the cells not filled with water. The game doesn’t ever stop. Your task is to find the minimum jump height the rabbit needs to have in order to survive for infinite time.
Input
First line of input contains 2 integer numbers n and m separated with a space (1 ≤ n, m ≤ 100, n · m ≠1 ) — size of the game grid.
The next n lines describe grid cells. Each of n strings contains m integer numbers, separated with a space hij (0 ≤ hij ≤ 105) — heights of cells.
The next line contains two integer numbers r1 and c1, separated with a space (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — initial location of the rabbit.
The last line contains two integers r2 and c2, separated with a space (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — initial location of water.
It is guaranteed, that the rabbit and water start in different cells. Columns are numerated from 1 to m from left to right. Rows are numerated from 1 to n from top to bottom.
Output
If the rabbit cannot survive in any case, in a single line output -1.
Otherwise, in a single line output a single number — the least jump height that rabbit should have in order to survive for infinite time.
Samples
input | output |
---|
2 3
6 5 4
1 2 3
2 1
1 3
| 3
|
1 3
0 0 0
1 1
1 3
| -1
|
Problem Author: Anton Lipin
Problem Source: University academic school olympiad in informatics 2019