#K76562. Knight's Minimum Moves

    ID: 34670 Type: Default 1000ms 256MiB

Knight's Minimum Moves

Knight's Minimum Moves

Given an \( n \times n \) chessboard, determine the minimum number of moves required for a knight to travel from a starting cell to a target cell. The knight moves in an L-shape: two steps in one direction and one step in a perpendicular direction. If the knight cannot reach the target cell, output \( -1 \).

For example, on an 8x8 board, the minimum number of moves from (0, 0) to (7, 7) is 6. Even if the starting and target cells are switched, the number of moves remains the same due to the symmetry of the chessboard.

You are given multiple test cases. For each test case, read the board size and the start and target positions, and output the minimum number of moves required for the knight.

inputFormat

The first line contains an integer \( T \), the number of test cases. Each of the next \( T \) lines contains five integers: \( n \) (the size of the board), \( start\_row \), \( start\_col \), \( target\_row \), and \( target\_col \). Each test case is processed independently.

Format:

T
n start_row start_col target_row target_col
n start_row start_col target_row target_col
... (T test cases total)

outputFormat

For each test case, output the minimum number of moves required for the knight to reach the target cell from the starting cell. If it is impossible, output \( -1 \). Each result should be printed on a new line.

Format:

result1
result2
... (T lines total)
## sample
3
8 0 0 7 7
8 0 0 0 1
8 0 0 3 3
6

3 2

</p>