H,W = gets.split.map(&:to_i)
ch,cw = gets.split.map{|i|i.to_i-1}
dh,dw = gets.split.map{|i|i.to_i-1}
S = []
H.times{
	S << gets.chomp
}
D = Array.new(H){Array.new(W){10**6}}
#warn D.inspect
q = [[ch,cw,0]]
until q.empty?
	h,w,d = q.shift
	next if D[h][w] <= d
	next if S[h][w] == "#"
	D[h][w] = d
	if h > 0
		q << [h-1,w,d]
	end
	if h < H - 1
		q << [h+1,w,d]
	end
	if w > 0
		q << [h,w-1,d]
	end
	if w < W - 1
		q << [h,w+1,d]
	end
	(h-2 .. h+2).each{|hh|
		next if hh < 0
		next if hh > H - 1
		(w-2 .. w+2).each{|ww|
			next if ww < 0
			next if ww > W - 1
			q << [hh,ww,d+1]
		}
	}
end
#warn D.inspect
if D[dh][dw] == 10**6
	puts -1
else
	puts D[dh][dw]
end
