import java.util.*

val dx = listOf(0, 1, 0, -1)
val dy = listOf(-1, 0, 1, 0)

fun main() {
  val (H, W) = readInts()
  val (Ch, Cw) = readInts().map { it - 1 }
  val (Dh, Dw) = readInts().map { it - 1 }
  val S = (1..H).map { readString() }

  val dist = Array(H) { Array(W) { Int.MAX_VALUE } }
  val deque = LinkedList<Pair<Int, Int>>()
  dist[Ch][Cw] = 0
  deque.add(Ch to Cw)
  while (deque.isNotEmpty()) {
    val (py, px) = deque.removeFirst()
    if (py to px == Dh to Dw) break
    // 通常移動
    for (i in 0..3) {
      val y = py + dy[i]
      val x = px + dx[i]
      if (y < 0 || y >= H || x < 0 || x >= W) continue
      if (S[y][x] == '#') continue
      val cost = dist[py][px] + 0
      if (dist[y][x] <= cost) continue
      dist[y][x] = cost
      deque.addFirst(y to x) // 先頭
    }
    // ワープ移動
    for (dy in -2..2) {
      for (dx in -2..2) {
        val y = py + dy
        val x = px + dx
        if (y < 0 || y >= H || x < 0 || x >= W) continue
        if (S[y][x] == '#') continue
        val cost = dist[py][px] + 1
        if (dist[y][x] <= cost) continue
        dist[y][x] = cost
        deque.addLast(y to x) // 後ろ
      }
    }
  }
  println(dist[Dh][Dw].let { if (it == Int.MAX_VALUE) -1 else it })
}

fun readString() = readLine()!!
fun readStrings() = readString().split(" ")
fun readInt() = readString().toInt()
fun readLong() = readString().toLong()
fun readInts() = readStrings().map { it.toInt() }
fun readLongs() = readStrings().map { it.toLong() }
