import scala.collection.mutable.ArrayDeque

object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val h, w = sc.nextInt()
  val ch, cw, dh, dw = sc.nextInt() - 1
  val s = Array.fill(h)(sc.next())

  def compose(x1: Int, x2: Int): Int = {
    x1 * w + x2
  }

  def decompose(n: Int): (Int, Int) = {
    val x1 = n / w
    val x2 = n % w
    (x1, x2)
  }

  val sp = Array.fill(h*w)(Int.MaxValue)
  sp(compose(ch, cw)) = 0
  val deque = ArrayDeque[Int]()
  deque.append(compose(ch, cw))
  while (deque.nonEmpty) {
    val (i1, j1) = decompose(deque.removeHead())
    val d = List((1,0), (-1, 0), (0,1), (0, -1))
    for ((di, dj) <- d) {
      val i2 = i1 + di
      val j2 = j1 + dj
      if (0 <= i2 && i2 < h && 0 <= j2 && j2 < w && s(i2)(j2) == '.' &&
        sp(compose(i2, j2)) > sp(compose(i1, j1))) {
        sp(compose(i2, j2)) = sp(compose(i1, j1))
        deque.prepend(compose(i2, j2))
      }
    }

    for {
      di <- -2 to 2
      dj <- -2 to 2
    } {
      val i2 = i1 + di
      val j2 = j1 + dj
      if (0 <= i2 && i2 < h && 0 <= j2 && j2 < w && s(i2)(j2) == '.'
        && sp(compose(i2, j2)) > sp(compose(i1, j1)) + 1) {
        sp(compose(i2, j2)) = sp(compose(i1, j1)) + 1
        deque.append(compose(i2, j2))
      }
    }
  }

  println(if (sp(compose(dh, dw)) == Int.MaxValue) -1 else sp(compose(dh, dw)))
}