~/algor $ cat problems/graf-ot-yurishi
Knight's shortest path
Medium
1000 ms
128 MB
49%
On an $n \times n$ chessboard a knight stands on cell $(r_1, c_1)$. In one move the knight shifts $2$ cells in one direction and $1$ cell in the perpendicular direction (an «L» shape) and cannot leave the board. What is the smallest number of moves for the knight to reach cell $(r_2, c_2)$? If it cannot get there, print $-1$.
Input
One line with five positive integers: $n$, $r_1$, $c_1$, $r_2$, $c_2$ (row and column numbers start from $1$).
Output
One integer — the smallest number of moves or $-1$.
Note
$1 \le n \le 200$
$1 \le r_1, c_1, r_2, c_2 \le n$
Examples
Input #1
8 1 1 8 8
Output #1
6
Input #2
3 2 2 1 1
Output #2
-1