h, w = gets.split.map &:to_i
ch, cw = gets.split.map &:to_i
dh, dw = gets.split.map &:to_i
s = h.times.map {gets.chomp}
graph = (h*w).times.map {[]}
h.times do |i|
  w.times do |j|
    5.times do |di|
      5.times do |dj|
        next if di == 2 and dj == 2
        pi, pj = i+di-2, j+dj-2
        next if pi < 0 or pi >= h or pj < 0 or pj >= w or s[pi][pj] == '#'
        graph[i*w+j] << [pi*w+pj, 1]
      end
    end
    graph[i*w+j] << [(i+1)*w+j, 0] if i+1 < h and s[i+1][j] == '.'
    graph[i*w+j] << [(i-1)*w+j, 0] if i > 1 and s[i-1][j] == '.'
    graph[i*w+j] << [i*w+j+1, 0] if j+1 < w and s[i][j+1] == '.'
    graph[i*w+j] << [i*w+j-1, 0] if j > w and s[i][j-1] == '.'
  end
end

dists = [10**9] * graph.size
dists[(ch-1)*w+cw-1] = 0
prevs = [nil] * graph.size
queue = [*0...graph.size]

until queue.empty?
  i = queue.min_by { |i| dists[i] }
  queue.delete i
  graph[i].each do |(j, c)|
    d = dists[i] + c
    if dists[j] > d
      dists[j] = d
      prevs[j] = i
    end
  end
end

=begin
graph.size.times do |i|
	graph[i].each do |(j, c)|
		if dists[j] > dists[i] + c
			dists[j] = dists[i] + c
          prevs[j] = [i, c]
		end
	end
end
=end

=begin
i = (dh-1)*w+dw-1
path = [[i/w, i-i/w, 0]]
while prevs[i] and path.size < h*w
  i, c = prevs[i]
  path << [i/w, i-i/w, c]
end
p path
=end

dist = dists[(dh-1)*w+dw-1]
if dist == 10**9 
  dist = -1
end
p dist