(H,W),(Ch,Cw),(Dh,Dw) = 3.times.map{ gets.split.map(&:to_i) }
S = H.times.map{ gets.chomp }

t,T = 0,Array.new(H){ [nil]*W }
Q,＃ = [*0...H].product([*0...W]),[]
until Q.empty?
	r,c = Q.pop
	next ＃ << [r,c] if S[r][c] == ?#

	T[r][c] = t+=1 unless T[r][c]

	[[r-1,c],[r+1,c],[r,c-1],[r,c+1]].each{|r2,c2|
		next if r2<0 or c2<0 or H<=r2 or W<=c2
		next if T[r2][c2] or S[r2][c2] != ?.

		T[r2][c2] = T[r][c]
		Q << [r2,c2]
	}
end

U = Array.new(t+1){ [false]*(t+1) }
Valid = lambda{|_|r,c=_; next 0<=r && r<H && 0<=c && c<W && T[r][c] }
＃.each{|r,c|
	if Valid[[r-1,c]] && t1=T[r-1][c]
		[[r,c-2],[r,c-1],[r,c+1],[r,c+2],[r+1,c-2],[r+1,c-1],[r+1,c],[r+1,c+1],[r+1,c+2]].select(&Valid).each{|r2,c2|
			if t1 != t2=T[r2][c2]
				U[t1][t2] = U[t2][t1] = true
			end
		}
	end
	if Valid[[r+1,c]] && t1=T[r+1][c]
		[[r,c-2],[r,c-1],[r,c+1],[r,c+2],[r-1,c-2],[r-1,c-1],[r-1,c],[r-1,c+1],[r-1,c+2]].select(&Valid).each{|r2,c2|
			if t1 != t2=T[r2][c2]
				U[t1][t2] = U[t2][t1] = true
			end
		}
	end
	if Valid[[r,c-1]] && t1=T[r][c-1]
		[[r-2,c],[r-1,c],[r+1,c],[r+2,c],[r-2,c+1],[r-1,c+1],[r,c+1],[r+1,c+1],[r+2,c+1]].select(&Valid).each{|r2,c2|
			if t1 != t2=T[r2][c2]
				U[t1][t2] = U[t2][t1] = true
			end
		}
	end
	if Valid[[r,c+1]] && t1=T[r][c+1]
		[[r-2,c],[r-1,c],[r+1,c],[r+2,c],[r-2,c-1],[r-1,c-1],[r,c-1],[r+1,c-1],[r+2,c-1]].select(&Valid).each{|r2,c2|
			if t1 != t2=T[r2][c2]
				U[t1][t2] = U[t2][t1] = true
			end
		}
	end
}

C,D = T[Ch-1][Cw-1],T[Dh-1][Dw-1]
turn = 0
q = [C]
V = [false]*(t+1)
until q.empty?
	r = []

	t1 = q.shift
	if t1 == D
		p turn
		exit
	end
	U[t1].each_with_index{|b,t2|
		next unless b
		next if V[t2]
		V[t2] = true
		r << t2
	}

	q = r
	turn += 1
end
p(-1)
