#C4760. Minimum Knight Moves on a Chessboard

    ID: 48334 Type: Default 1000ms 256MiB

Minimum Knight Moves on a Chessboard

Minimum Knight Moves on a Chessboard

You are given a chessboard of size (N \times M) and a knight placed on the board at coordinates ((sx, sy)). The knight can move in an "L" shape: two cells in one direction and one cell perpendicular to that. Formally, from a cell ((x, y)), the knight can move to any cell ((x+dx, y+dy)) where ((dx, dy)) is one of the following eight moves: ({(-2,-1), (-1,-2), (1,-2), (2,-1), (2,1), (1,2), (-1,2), (-2,1)}).

Your task is to determine the minimum number of moves required for the knight to travel from the starting position ((sx, sy)) to the target position ((tx, ty)). If it is impossible for the knight to reach the target, output (-1).

inputFormat

The input consists of a single line containing six space-separated integers: (N), (M), (sx), (sy), (tx), and (ty), where:

  • (N, M) are the dimensions of the chessboard (number of rows and columns respectively),
  • (sx, sy) are the coordinates of the starting position,
  • (tx, ty) are the coordinates of the target position. It is guaranteed that (1 \leq sx, sy, tx, ty \leq N, M).

outputFormat

Output a single integer, which is the minimum number of moves required for the knight to reach the target cell. If the target cannot be reached, output (-1).## sample

5 5 1 1 5 5
4