Knight Path on a Blocked Board


Submit solution

Points: 100
Time limit: 2.0s
Memory limit: 256M

Author:
Problem types

A knight may land only on cells with value 1. Find its minimum number of moves from (s,t) to (u,v). Coordinates are one-based.

Input

The first line contains n. The second line contains s, t, u, v. The next n lines contain the board.

  • 2 <= n <= 1000

Output

Print the minimum moves, or -1 if the target is unreachable.

Example

Input
5
1 1 5 5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Output
4

Comments

There are no comments at the moment.