Problems
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
At the beginning of his career, Milan revealed to Matej two magic numbers a and b 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 and determine the minimum possible distance between the price of the plot of land and the two magic numbers.
Input
The first line contains positive integers
The
Output
In the only line print the minimum possible distance between the price of the plot of land and the two magic numbers.
Subtask
| # | Score | Condition |
|---|---|---|
| #1 | 25 | |
| #2 | 26 | |
| #3 | 49 | No additional constraints. |
Example #1
2 2 10 10
1 3
4 1
2
Example #2
3 2 3 4
1 9
1 1
8 1
3
Matej can buy a plot of land consisting of two adjacent cells of cost 1. The total price is then 1 + 1 = 2, and the distance between it and the magic numbers is |3 − 2| + |4 − 2| = 3.
Example #3
3 4 5 3
1 1 1 1
9 6 7 6
8 1 9 7
2