Steps by Knight
Medium Accuracy: 44.64% Submissions: 16713 Points: 4

Given a square chessboard of N x N size, the position of Knight and position of a target is given. We need to find out minimum steps a Knight will take to reach the target position.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the square chessboard. The next line contains the X-Y coordinates of the knight. The next line contains the X-Y coordinates of the target.

Output:
Print the minimum steps the Knight will take to reach the target position.

Constraints:
1<=T<=100
1<=N<=20
1<=knight_pos,targer_pos<=N

Example:
Input:

2
6
4 5
1 1
20
5 7
15 20

Output:
3
9

Explanation:
Testcase 1:

The most optimal path is: (4,5) -> (2,4) -> (3,2) -> (1,1).
Hence, we can reach the destination in 3 steps.

to report an issue on this page.

### Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?