import scala.collection.mutable.Set
import scala.collection.mutable.ListBuffer

object Main {
  class Solver(sizeh:Int, sizew:Int,
               ch:Int, cw:Int,
               dh:Int, dw:Int,
               maze: Array[Array[Char]]
              ) {
    def inMaze(h:Int,w:Int) = {
      (h >= 0 && h < sizeh && w >= 0 && w < sizew && maze(h)(w) == '.')
    }

    def solve2():Int = {
      var q1 = Set.empty[Tuple2[Int, Int]]
      var q2 = Set.empty[Tuple2[Int, Int]]

      q1 += Tuple2(ch, cw)

      var warp=0


      while (q1.nonEmpty || q2.nonEmpty) {
        if (q1.isEmpty) {
          q1 = q2
          q2 = Set.empty[Tuple2[Int, Int]]
          warp = warp + 1
        }

        val (headh, headw) = q1.head
        q1 = q1.tail

        if (headh == dh && headw == dw) {
          return warp
        }

        if (maze(headh)(headw) == '.') {
          maze(headh)(headw) = '*'
          val nonW = List((-1, 0), (1, 0), (0, -1), (0, 1))
          nonW.foreach {
            case (hh, ww) => {
              val h = headh + hh
              val w = headw + ww

              if (inMaze(h, w)) {
                q1.add(Tuple2(h, w))
              }
            }
          }
          Range(-2, 3).map(hh => {
            Range(-2, 3).map(ww => {
              (hh, ww)
            })
          }).flatten.toList.filter(l => {
            l != (0, 0) && (!nonW.contains(l))
          }).foreach {
            case (hh, ww) => {
              val h = headh + hh
              val w = headw + ww

              if (inMaze(h, w)) {
                q2.add(Tuple2(h, w))
              }
            }
          }
        }
      }
      return -1
    }



    def solve():Int = {
      var q = new ListBuffer[Tuple3[Int, Int, Int]]

      q += Tuple3(ch, cw, 0)

      while (q.nonEmpty) {
        val (headh, headw, headwarp) = q(0)

        q.remove(0)
        if (headh == dh && headw == dw) {
          return headwarp
        }

        if (maze(headh)(headw) == '.') {
          maze(headh)(headw) = '*'
          val nonW = List((-1, 0), (1, 0), (0, -1), (0, 1))
          nonW.foreach {
            case (hh, ww) => {
              val h = headh + hh
              val w = headw + ww

              if (inMaze(h, w)) {
                q.insert(0, (h, w, headwarp))
              }
            }
          }
          Range(-2, 3).map(hh => {
            Range(-2, 3).map(ww => {
              (hh, ww)
            })
          }).flatten.toList.filter(l => {
            l != (0, 0) && (!nonW.contains(l))
          }).foreach {
            case (hh, ww) => {
              val h = headh + hh
              val w = headw + ww

              if (inMaze(h, w)) {
                q += Tuple3(h, w, headwarp + 1)
              }
            }
          }
        }
      }
      return -1
    }
  }
  def main(args: Array[String]) = {
//    val ls = test1()
    val ls = fromStdin()
    run(ls)
  }

  def test1() = {
    val lines = List(
      "4 4",
      "1 1",
      "4 4",
      "..#.",
      "..#.",
      ".#..",
      ".#.."
    )
    lines
  }

  def test2() = {
    val lines = List("4 4",
      "1 4",
      "4 1",
      ".##.",
      "####",
      "####",
      ".##.")
    lines
  }

  def fromStdin() = {
    val l1 = io.StdIn.readLine()
    val n = l1.split(" ")(0).toInt + 2
    val lines = List(l1) ++ (0 until n).map(x => {
      io.StdIn.readLine()
    }).toList

    lines
  }

  def shift(lines: List[String]) = {
    Tuple2(lines.head, lines.tail)
  }

  def run(lines: List[String]) = {
    var ls = lines

    var l = ls.head.split(" ").map(_.toInt)
    ls = ls.tail
    val sizeh = l(0)
    val sizew = l(1)

    l = ls.head.split(" ").map(_.toInt)
    ls = ls.tail
    val ch = l(0)-1
    val cw = l(1)-1

    l = ls.head.split(" ").map(_.toInt)
    ls = ls.tail
    val dh = l(0)-1
    val dw = l(1)-1

    val maze = ls.map(l=>{
      l.toCharArray
    }).toArray

    val solver = new Solver(sizeh,sizew,
      ch, cw,
      dh,dw,
      maze)
    val r = solver.solve2()

    println(r)
  }

}
