#K35667. Knight's Minimum Moves on Chessboard

    ID: 25583 Type: Default 1000ms 256MiB

Knight's Minimum Moves on Chessboard

Knight's Minimum Moves on Chessboard

You are given an \(N \times N\) chessboard and a knight's starting position \((s_x, s_y)\) and target position \((e_x, e_y)\). The knight moves in an L-shape as in standard chess: two squares in one direction and then one square perpendicular to that.

Your task is to determine the minimum number of moves required for the knight to reach the target position. If the target position is unreachable from the starting position, output -1.

The knight's moves can be mathematically represented by the set of moves: \(\{(-2,-1),\ (-1,-2),\ (1,-2),\ (2,-1),\ (2,1),\ (1,2),\ (-1,2),\ (-2,1)\}\).

inputFormat

The input consists of several lines. Each line (except the last) contains five space-separated integers:

  • N: the size of the chessboard (i.e., the board is \(N \times N\)).
  • s_x and s_y: the starting coordinates of the knight (1-indexed).
  • e_x and e_y: the target coordinates (1-indexed).

The input terminates with a line containing a single 0, which should not be processed.

outputFormat

For each test case, output a single integer on a separate line representing the minimum number of moves required for the knight to reach the target position. If it is impossible to reach the target, output -1.

## sample
8 1 1 8 8
0
6

</p>