Longest walk
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Zenyk and Marichka are walking on a rectangular field of size \(n\) by \(m\) meters, divided into cells of size \(1\) by \(1\) meter.
Initially, the couple is in the cell \((x_1, y_1)\), and they want to end up in the cell \((x_2, y_2)\). In one hour, Zenyk and Marichka choose one of four directions (forward, backward, right, left), move in this direction, and stop if:
Zenyk and Marichka are in the cell \((x_2, y_2)\), in which case they also finish the walk.
The next cell in this direction has already been visited before.
They have reached the edge of the field.
They can choose a direction only if they will move at least one cell in that direction. Now they are wondering what is the maximum number of hours the walk can last before they reach the desired cell.
Input
The first line contains two integers \(n\) and \(m\) — the sizes of the field.
The second line contains two integers \(x_1\) and \(y_1\) — the starting cell.
The third line contains two integers \(x_2\) and \(y_2\) — the ending cell.
Output
Output a single integer — the maximum length of the walk.
Constraints
\(2 \le n , m \le 100\),
\(1 \le x_1, x_2 \le n\),
\(1 \le y_1, y_2 \le m\),
\((x_1, y_1) \ne (x_2, y_2)\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 7 4 3 2 3 3 | 8 |
Notes
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|