import java.util.*
//val allNeightBours = mutableMapOf<Pair<Int, Int>,Boolean>()
val map = mutableListOf<String>()
var H = 0
var W = 0
var s = Pair(0,0)
var t = Pair(0,0)
fun main(args: Array<String>) {
    val reader = Scanner(System.`in`)

    H = reader.nextInt()
    W = reader.nextInt()
    s = Pair(reader.nextInt() - 1, reader.nextInt() - 1)
    t = Pair(reader.nextInt() - 1, reader.nextInt() - 1)

    var allNeightBours = Array(H){
        val elms = IntArray(W)
            elms.fill(0)
        elms
    }

    repeat(H){
        map.add(reader.next())
    }
    val neighboursQueue = ArrayDeque<Pair<Int, Int>>()

    neighboursQueue.push(s)
    allNeightBours[s.first][s.second] = 1
    var swapNum = 0
    var findTarget = false

    var checkedNeighbous = mutableListOf<Pair<Int, Int>>()
    while(neighboursQueue.size > 0 && findTarget.not()){
        val checkPoint = neighboursQueue.pop()
        checkedNeighbous.add(checkPoint)
        listOf(Pair(1, 0), Pair(-1, 0), Pair(0, 1), Pair(0, -1)).forEach nearloop@{
            var newPoint = Pair(checkPoint.first + it.first, checkPoint.second + it.second)
            if ((newPoint.first >= 0 && newPoint.first < H && newPoint.second >= 0 && newPoint.second < W) &&
                allNeightBours[newPoint.first][newPoint.second] == 0 &&
                map[newPoint.first][newPoint.second] == '.'
            ) {
                if(newPoint == t){
                    findTarget = true
                    return@nearloop
                }
                else {
                    neighboursQueue.add(newPoint)
                    allNeightBours[newPoint.first][newPoint.second] = 1
                }
            }
        }
        if(findTarget){
            break
        }
        if(neighboursQueue.size == 0){
            swapNum+= 1
            checkedNeighbous.forEach farloop@{neighbour ->
                repeat(5){countH->
                    repeat(5){ countW->
                        val tempH = neighbour.first - 2 + countH
                        val tempW = neighbour.second - 2 + countW

                        if((tempH < 0 || tempH >= H || tempW < 0 || tempW >= W) ||
                            allNeightBours[tempH][tempW] == 1 ||
                            map[tempH][tempW] != '.'){

                        }
                        else {
                            if(tempH == t.first && tempW == t.second){
                                findTarget = true
                                return@farloop
                            }
                            else {
                                val item = Pair(tempH, tempW)
                                neighboursQueue.add(item)
                                allNeightBours[item.first][item.second] = 1
                            }
                        }
                    }
                }
            }
            checkedNeighbous.clear()
        }
    }
    println(if(findTarget)swapNum else -1)
}