Knight's Marathon
Time limit2sMemory limit512 MB
On a huge rectangular board, find the minimum number of knight moves from one square to another while staying inside the board.
Problem
The invading army is beaten at last. The king sends his only surviving knight to the capital to tell the people about the victory. The trip may be a very long one.
The knight moves the way a knight moves on a chessboard. In one move he travels two squares in one of the four compass directions, then one more square at a right angle to that direction. He must stay inside the kingdom for the whole trip so that he does not start a new war. The kingdom is a rectangular grid of size , possibly much larger than the board the battle was fought on. Rows and columns are numbered from 0. The knight starts at square and must reach the capital at square . Find the smallest number of moves in which the knight can reach the capital.

Figure 1: the squares a knight can reach in one move.
Input
The input has three lines, each with two integers.
- The first line has the size of the kingdom, and ().
- The second line has the starting position of the knight, and (, ).
- The third line has the position of the capital, and (, ).
Output
Print one line with the smallest number of moves the knight needs to reach the capital.