
Matej is, as is well known, the biggest Croatian innovator and businessman. His company
is expanding, which is why he decided to buy a plot of land near Velika Gorica. The
available land is a rectangle-shaped region consisting of square unit cells. Each
cell has its own price and it's not possible to purchase only a part of a cell. Matej is
an experienced businessman and he knows that the key to success is not simply to buy
the biggest plot of land or the least expensive one. Instead, he should buy a plot of
land whose price is as close as possible to the magic numbers given to him by Milan the
psychic.
At the beginning of his career, Milan revealed to Matej two magic numbers and
crucial for commercial
success. Therefore, Matej wishes to buy a (nonempty) rectangular plot of land so that the distance
between its price and the magic numbers is as small as possible. The distance between the price and a
single magic number is just the absolute value of their difference, and the distance between the price
and both of the magic numbers is the sum of these absolute differences. Help Matej determine the
minimum possible distance between the price of the plot of land and the two magic numbers.
Input Specification
The first line contains positive integers ,
,
, and
from the problem.
The of the following
lines contains a sequence of
positive integers
, the prices of
the individual cells, in order.
Output Specification
In the only line, print the minimum possible distance between the price of the plot of land and the two magic numbers.
Constraints
Subtask | Points | Constraints |
---|---|---|
No additional constraints. |
Sample Input 1
2 2 10 10
1 3
4 1
Sample Output 1
2
Sample Input 2
3 2 3 4
1 9
1 1
8 1
Sample Output 2
3
Explanation for Sample Output 2
Matej can buy a plot of land consisting of two adjacent cells of cost . The total price is then
,
and the distance between it and the magic numbers is
.
Sample Input 3
3 4 5 3
1 1 1 1
9 6 7 6
8 1 9 7
Sample Output 3
2
Comments