import java.util.Scanner

import scala.collection.mutable

object Main extends App {
  val sc = new Scanner(System.in)
  val H, W = sc.nextInt
  val Ch, Cw = sc.nextInt
  val Dh, Dw = sc.nextInt
  sc.nextLine
  val S = Array.fill(H)(sc.nextLine.map(_ == '.'))

  val Q = mutable.Queue.empty[(Int, Int)]
  val visited = Array.fill(H, W)(0)
  val dp = Array.fill(H, W)(Int.MaxValue)
  visited(Ch-1)(Cw-1) = 2
  dp(Ch-1)(Cw-1) = 0
  Q.enqueue((Ch-1, Cw-1))
  while (Q.nonEmpty) {
    val (vx, vy) = Q.dequeue
    Seq(
      (vx - 1, vy, false), (vx + 1, vy, false), (vx, vy - 1, false), (vx, vy + 1, false),
      (vx - 2, vy - 2, true), (vx - 2, vy - 1, true), (vx - 2, vy, true), (vx - 2, vy + 1, true), (vx - 2, vy + 2, true),
      (vx - 1, vy - 2, true), (vx - 1, vy - 1, true), (vx - 1, vy + 1, true), (vx - 1, vy + 2, true),
      (vx, vy - 2, true), (vx, vy + 2, true),
      (vx + 1, vy - 2, true), (vx + 1, vy - 1, true), (vx + 1, vy + 1, true), (vx + 1, vy + 2, true),
      (vx + 2, vy - 2, true), (vx + 2, vy - 1, true), (vx + 2, vy, true), (vx + 2, vy + 1, true), (vx + 2, vy + 2, true)
    )
      .filter { case (x, y, _) => x >= 0 && x < H && y >= 0 && y < W && S(x)(y) }
      .foreach { case (x, y, useMagic) =>
        if (useMagic && visited(x)(y) == 0 || visited(x)(y) < 2) {
          visited(x)(y) = if (useMagic) 1 else 2
          dp(x)(y) = math.min(dp(x)(y), dp(vx)(vy) + (if (useMagic) 1 else 0))
          Q.enqueue((x, y))
        }
      }
  }

  val z = dp(Dh-1)(Dw-1)

  println(if (z == Int.MaxValue) -1 else z)
}
