Garbage Collection
Your team follows the black hole, and after several days of travel, you arrive.
You gaze upon TRAPPIST-1, in all its glory. The dim glow of the red dwarf bathes its Earth-like planets, each one more breathtaking than the last. But then, another object appears. A ship?
The spacecraft leaps toward you at light speed, and suddenly, something surrounds you. A cuboidal net of pure energy locks you into a rectangular region of space. From within the alien vessel, unfamiliar beings observe you with cold indifference.
You are confined to a 2D rectangular grid with rows and
columns. You start at row
and column
, i.e.
. Each minute, two things occur:
- You must move your ship one square in any of the four cardinal directions (up, down, left, right), as long as you remain within the bounds of the grid.
- Then, the aliens move one of the four edges of the rectangular net inward, shrinking your available space.
Once the grid has been reduced to a cell, your team will be captured and taken aboard the alien ship.
Given that your team moves optimally to survive as long as possible, and the aliens move optimally to capture you as quickly as possible, how many minutes can your team avoid capture?
Input
The first line consists of two integers
and
, the initial size of the net.
The second line consists of two integers
and
, representing that your initial position in the net is at row
and column
.
Output
Output the maximum number of minutes you can survive (same as the maximum number of moves you can make) before your team is captured by the aliens.
Example
Input 1
4 4
1 0
Output 1
4
With optimal play, your team can survive for minutes. Note, this is not the only way to achieve such a time.
Input 2
3 3
1 1
Output 2
2
Input 3
3 3
1 2
Output 3
3
Comments