import scala.io.StdIn._

object Main extends App {

  val Array(h,w) = readLine().split(" ").map(_.toInt)
  val Array(ch,cw) = readLine().split(" ").map(_.toInt-1)
  val Array(dh,dw) = readLine().split(" ").map(_.toInt-1)
  val S = Array.fill(h)(readLine().split(""))

  var ans = Int.MaxValue
  var canGoal = false
  def solve() = {
    val visitedArr = Array.fill(h,w)(false)
    dfs((ch,cw), 0, ".", visitedArr)
    if(canGoal) ans else -1
  }

  def dfs(cur: (Int, Int), warpCnt: Int, prev: String, visited: Array[Array[Boolean]],
    over: Boolean = false): Unit = {
    if(cur == (dh,dw)) {
      ans = Math.min(ans, warpCnt)
      canGoal = true
    }
    else if(over) ()
    else {
      val (x,y) = cur
      for {
        nx <- x-1 to x+1 if 0 <= nx && nx < h
        ny <- y-1 to y+1 if 0 <= ny && ny < w
        if !visited(nx)(ny)
      } {
        val updateVisited = visited.updated(nx, visited(nx).updated(ny, true))
        // not use warp
        if(S(nx)(ny) == "." && !isWarped((x,y), (nx,ny))) dfs((nx,ny), warpCnt, ".", updateVisited)
        // use warp
        else {
          if(prev == ".") dfs((nx,ny), warpCnt+1, "#", updateVisited)
          else dfs((nx,ny), warpCnt+1, "#", updateVisited, over = true)
        }
      }
    }
  }

  def isWarped(cur: (Int, Int), next: (Int, Int)): Boolean = {
    val (x,y) = cur
    val (nx, ny) = next
    if((nx-x).abs == 1 && (ny-y).abs == 1) true else false
  }

  println(solve())

}

