fun list() = readLine()!!.split(" ").map {it.toInt()}

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

fun main() {
	val (h,w) = list()
	val (ch,cw) = list()
	val (dh,dw) = list()

	val a = Array(h) {readLine()!!}
	val r = Array(h) {IntArray(w) {Int.MAX_VALUE/2}}

	val q = java.util.ArrayDeque<Triple<Int,Int,Int>>()
	r[ch-1][cw-1] = 0
	q.add(Triple(ch-1,cw-1,0))

	fun isRoad(y:Int,x:Int) = y in 0 until h&&x in 0 until w&&a[y][x]=='.'

	fun f(y:Int,x:Int,v:Int) {
		for(d in 0..3) {
			val nx = x+dx[d]
			val ny = y+dy[d]
			if(isRoad(ny,nx)&&r[ny][nx]>v) {
				r[ny][nx] = v
				q.addFirst(Triple(ny,nx,v))
			}
		}

		for(dy in -2..2) {
			for(dx in -2..2) {
				val nx = x+dx
				val ny = y+dy
				if(isRoad(ny,nx)&&r[ny][nx]>v+1) {
					r[ny][nx] = v+1
					q.addLast(Triple(ny,nx,v+1))
				}
			}
		}
	}

	while(!q.isEmpty()) {
		val (y,x,v) = q.poll()
		f(y,x,v)
	}

	println(r[dh-1][dw-1].let {if(it==Int.MAX_VALUE/2) -1 else it})
}
