import java.util.ArrayDeque
import kotlin.Comparator
import kotlin.String

fun main(args: Array<String>) {
    val (h, w) = readIntList()
    val (ch, cw) = readIntList().map { it - 1 }
    val (dh, dw) = readIntList().map { it - 1 }
    val s = List(h) { readString() }

    val q = ArrayDeque<Pair<Int, Int>>()
    q.addLast(ch to cw)

    val ans = List(h) { MutableList(w) { Int.MAX_VALUE } }
    ans[ch][cw] = 0

    val af = listOf(Pair(0, 1), Pair(0, -1), Pair(1, 0), Pair(-1, 0))

    while (q.isNotEmpty()) {
        val (x, y) = q.removeFirst()

        af.forEach { (xf, yf) ->
            val tx = x + xf
            val ty = y + yf
            if (tx >= 0 && ty >= 0 && tx < h && ty < w && s[tx][ty] == '.' && ans[x][y] < ans[tx][ty]) {
                ans[tx][ty] = ans[x][y]
                q.addFirst(tx to ty)
            }
        }

        for (xf in -2..2) {
            for (yf in -2..2) {
                val tx = x + xf
                val ty = y + yf

                if (tx == x && ty == y) continue
                if (tx == x + 1 && ty == y) continue
                if (tx == x - 1 && ty == y) continue
                if (tx == x && ty == y + 1) continue
                if (tx == x && ty == y - 1) continue

                if (tx >= 0 && ty >= 0 && tx < h && ty < w && s[tx][ty] == '.' && ans[x][y] + 1 < ans[tx][ty]) {
                    ans[tx][ty] = ans[x][y] + 1
                    q.addLast(tx to ty)
                }
            }

        }
    }

    if (ans[dh][dw] == Int.MAX_VALUE) {
        println(-1)
    } else {
        println(ans[dh][dw])
    }
}

// read
fun readString() = readLine()!!
fun readInt() = readLine()!!.toInt()
fun readLong() = readLine()!!.toLong()
fun readStringList() = readLine()!!.split(" ")
fun readIntList() = readLine()!!.split(" ").map(String::toInt)
fun readLongList() = readLine()!!.split(" ").map(String::toLong)