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' = VU.map toMaze 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
        VUM.write maze (from2d i j) v
        forM_ [(i+1,j), (i-1,j), (i,j+1), (i,j-1)] $ \(i', j') ->
          when (inBound i' j') $ do
            c <- VUM.read maze (from2d i' j')
            case c of
              999999 -> return ()
              -1 -> dfs maze (i', j') v
              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+2,j+1), (i-2,j-1), (i-2,j+1), (i+2,j-1), (i+1,j+2), (i-1,j-2), (i-1,j+2), (i+1,j-2)] $ \(i', j') ->
          when (inBound i' j') $ do
            c <- VUM.read maze (from2d i' j')
            case c of
              999999 -> return ()
              -1 -> dfs maze (i', j') (v+1)
              w -> if v >= w then return () else dfs maze (i', j') (v+1)
  mat'' <- VU.thaw mat'
  ans <- dfs mat'' start 0
  v <- VUM.read mat'' (uncurry from2d goal)
  -- debug <- VU.freeze mat''
  -- print2d w h debug
  print $ case v of
    999999 -> -1
    v -> v

toMaze :: Char -> Int
toMaze '#' = 999999
toMaze '.' = -1


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