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 NX×NY, possibly much larger than the 8×8 board the battle was fought on. Rows and columns are numbered from 0. The knight starts at square (KX,KY) and must reach the capital at square (CX,CY). 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.