Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/graf-ot-yurishi

Knight's shortest path

Medium 1000 ms 128 MB 49%
Share

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$.

A chessboard: the shortest sequence of knight moves from the starting cell to the target

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

Sign in to submit a solution

Signing up is free and takes a minute.