#P5372. Domino Puzzle Transformation

    ID: 18605 Type: Default 1000ms 256MiB

Domino Puzzle Transformation

Domino Puzzle Transformation

You are given an n by m grid board where both n and m are odd numbers. The grid is completely covered by 1\times 2 dominoes except for one empty cell. In other words, there are exactly \(\frac{nm-1}{2}\) dominoes placed on the grid. Dominoes may be rotated, but they cannot overlap.

You are allowed to perform exactly two types of operations:

  1. Rotate a domino that is adjacent (sharing a common edge) to the empty cell by \(90^\circ\) such that part of the domino moves into the empty cell.
  2. Slide a domino that is adjacent to the empty cell into the empty cell.

For the purpose of this problem, the effect of these operations is modeled by moving the empty cell on the grid. In one move, the empty cell may shift either 1 unit or 2 units in one of the four cardinal directions (up, down, left, or right), as long as it remains within the boundaries of the board.

Given the initial and target positions of the empty cell, your task is to compute the minimum number of moves required to transform the grid from the initial state to the target state. Note that if the empty cell moves a total vertical distance of dr and a horizontal distance of dc, then the minimum number of moves is given by:

[ \lceil \frac{d_r}{2} \rceil + \lceil \frac{d_c}{2} \rceil ]

Here, \(\lceil x \rceil\) denotes the ceiling function, which rounds \(x\) up to the nearest integer.

inputFormat

The first line contains two odd integers n and m (\(1 \leq n, m \leq 10^9\)) representing the number of rows and columns of the grid.

The second line contains two integers r1 and c1 (1-indexed) specifying the initial position of the empty cell.

The third line contains two integers r2 and c2 (1-indexed) specifying the target position of the empty cell.

outputFormat

Output a single integer — the minimum number of moves required to transform the grid from the initial state to the target state using the allowed moves.

sample

5 5
3 3
3 3
0

</p>