Problem:
Score : 400 points

Problem Statement

A maze is composed of a grid of H \times W squares - H vertical, W horizontal.

The square at the i-th row from the top and the j-th column from the left - (i,j) - is a wall if S_{ij} is # and a road if S_{ij} is ..

There is a magician in (C_h,C_w). He can do the following two kinds of moves:

Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.

Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in.

In either case, he cannot go out of the maze.

At least how many times does he need to use the magic to reach (D_h, D_w)?

Constraints

1 \leq H,W \leq 10^3

1 \leq C_h,D_h \leq H

1 \leq C_w,D_w \leq W

S_{ij} is # or ..

S_{C_h C_w} and S_{D_h D_w} are ..

(C_h,C_w) \neq (D_h,D_w)

Input

Input is given from Standard Input in the following format:

H W
C_h C_w
D_h D_w
S_{11}\ldots S_{1W}
\vdots
S_{H1}\ldots S_{HW}

Output

Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print -1 instead.

Sample Input 1

4 4
1 1
4 4
..#.
..#.
.#..
.#..

Sample Output 1

1

For example, by walking to (2,2) and then using the magic to travel to (4,4), just one use of magic is enough.

Note that he cannot walk diagonally.

Sample Input 2

4 4
1 4
4 1
.##.
####
####
.##.

Sample Output 2

-1

He cannot move from there.

Sample Input 3

4 4
2 2
3 3
....
....
....
....

Sample Output 3

0

No use of magic is needed.

Sample Input 4

4 5
1 2
2 5
#.###
####.
#..##
#..##

Sample Output 4

2

Solution in Haskell (GHC 8.8.3) (judge verdict: Wrong Answer)

module Main where
import qualified Data.ByteString.Char8         as BS
import qualified Data.ByteString.Lazy.Char8    as BSL
import qualified Data.ByteString.Builder       as BSB
import           Data.Char                      ( isSpace )
import           Data.List                      ( unfoldr )
import qualified Data.Vector                   as V
import qualified Data.Vector.Mutable           as VM
import qualified Data.Vector.Unboxed           as VU
import qualified Data.Vector.Unboxed.Mutable   as VUM
import qualified Data.Vector.Generic           as VG
import qualified Data.Vector.Generic.Mutable   as VGM
import           Data.Maybe
import           Control.Monad

main :: IO ()
main = do
  [h, w] <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  [ch, cw] <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  [dh, dw] <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  mat <- VU.unfoldrN (h*w) (BS.uncons . BS.dropWhile isSpace) <$> BS.getContents
  let mat' = V.map toMaze $ VU.convert mat
  let from2d = mkFrom2d w h
  let to2d = mkTo2d w h
  let inBound = mkInBound w h
  let start = (cw-1, ch-1)
  let goal = (dw-1, dh-1)
  let dfs maze (i, j) v = do
        VM.write maze (from2d i j) (Cost v)
        forM_ [(i+1,j), (i-1,j), (i,j+1), (i,j-1)] $ \(i', j') ->
          when (inBound i' j') $ do
            c <- VM.read maze (from2d i' j')
            case c of
              Wall -> return ()
              Path -> dfs maze (i', j') v
              Cost w -> if v >= w then return () else dfs maze (i', j') v
        forM_ [(i+2,j), (i-2,j), (i,j+2), (i,j-2), (i+2,j+2), (i-2,j-2), (i-2,j+2), (i+2,j-2)] $ \(i', j') ->
          when (inBound i' j') $ do
            c <- VM.read maze (from2d i' j')
            case c of
              Wall -> return ()
              Path -> dfs maze (i', j') (v+1)
              Cost vv -> if v >= vv then return () else dfs maze (i', j') (v+1)
  mat'' <- V.thaw mat'
  ans <- dfs mat'' start 0
  v <- VM.read mat'' (uncurry from2d goal)
  -- debug <- V.freeze mat''
  -- print2d w h debug
  print $ case v of
    Wall -> -1
    Path -> -1
    Cost a -> a

data Maze = Wall | Path | Cost Int deriving Show
toMaze '#' = Wall
toMaze '.' = Path


-- usage:
-- do
--   [x, y] <- getSizeAction 
--   let from2d = mkFrom2d x y
--   let to2d = mkTo2d x y
--   let inBound = mkInBound x y
--   let v ! i j = mk2dIndexing x y
--   let v !? i j = mk2dIndexingMaybe x y
mkFrom2d :: Int -> Int -> Int -> Int -> Int
mkFrom2d x y i j = i + x * j

mkTo2d :: Int -> Int -> Int -> (Int, Int)
mkTo2d x y idx = (idx `mod` x, idx `div` x)

mkInBound :: Int -> Int -> Int -> Int -> Bool
mkInBound x y i j = 0 <= i && i < x && 0 <= j && j < y

mk2dIndexing :: VG.Vector v a => Int -> Int -> v a -> Int -> Int -> a
mk2dIndexing x y v i j = v VG.! mkFrom2d x y i j

mk2dIndexingMaybe :: VG.Vector v a => Int -> Int -> v a -> Int -> Int -> Maybe a
mk2dIndexingMaybe x y v i j
  | 0 <= i && i < x && 0 <= j && j < y = Just $ mk2dIndexing x y v i j
  | otherwise                          = Nothing

putYN :: Bool -> IO ()
putYN True  = putStrLn "Yes"
putYN False = putStrLn "No"

print2d :: (Show (v a), Foldable v, VG.Vector v a) => Int -> Int -> v a -> IO ()
print2d x y v = mapM_ print $ chunksOf x v
  where
    chunksOf k = go
      where
        go t = case VG.splitAt k t of
                (a,b) | null a    -> []
                      | otherwise -> a : go b