Huge Knight
Time limit1sMemory limit128 MB
Given two squares on an N by N board with N up to 10^15, find the minimum number of knight moves between them.
Problem
You have a chessboard of size . The rows and the columns are numbered from 1 to . A knight starts on the cell in row , column , and it wants to reach the cell in row , column .
One knight move goes two cells along one axis and one cell along the other. A knight on can move to , , , , , , , or . The knight cannot leave the board.
Given , , , , and , find the minimum number of moves that takes the knight from to .
Input
The first line contains a positive integer , the number of test cases.
Each test case is one line with five integers , , , , . Here , and , , , are between 1 and inclusive.
Output
For each test case, print the minimum number of moves that takes the knight from to , one per line.
Assume a solution always exists, so every input gives a destination the knight can reach from its starting cell.