This page is still under construction.

Parts of this page are still being built. What you see may change.

Huge Knight

Time limit1sMemory limit128 MB

Summary
Given two squares on an N by N board with N up to 10^15, find the minimum number of knight moves between them.
Level

Medium7 of 10

Topics
Math, BFS
Solved
No attempts yet

Problem

You have a chessboard of size N×NN \times N. The rows and the columns are numbered from 1 to NN. A knight starts on the cell in row R1R_1, column C1C_1, and it wants to reach the cell in row R2R_2, column C2C_2.

One knight move goes two cells along one axis and one cell along the other. A knight on (A,B)(A, B) can move to (A−2,B−1)(A-2, B-1), (A−2,B+1)(A-2, B+1), (A+2,B−1)(A+2, B-1), (A+2,B+1)(A+2, B+1), (A−1,B−2)(A-1, B-2), (A+1,B−2)(A+1, B-2), (A−1,B+2)(A-1, B+2), or (A+1,B+2)(A+1, B+2). The knight cannot leave the board.

Given NN, R1R_1, C1C_1, R2R_2, and C2C_2, find the minimum number of moves that takes the knight from (R1,C1)(R_1, C_1) to (R2,C2)(R_2, C_2).

Input

The first line contains a positive integer TT, the number of test cases.

Each test case is one line with five integers NN, R1R_1, C1C_1, R2R_2, C2C_2. Here 3≤N≤10153 \le N \le 10^{15}, and R1R_1, C1C_1, R2R_2, C2C_2 are between 1 and NN inclusive.

Output

For each test case, print the minimum number of moves that takes the knight from (R1,C1)(R_1, C_1) to (R2,C2)(R_2, C_2), one per line.

Assume a solution always exists, so every input gives a destination the knight can reach from its starting cell.

Examples1

  1. Example 1

    Input
    2
    5 1 1 2 3
    5 1 1 2 2
    
    Expected output
    1
    4