h,w = gets.chomp.split(" ").map(&:to_i)
c = gets.chomp.split(" ").map(&:to_i)
d = gets.chomp.split(" ").map(&:to_i)
s = Array.new(h){Array.new}
h.times do |i|
  s[i] = gets.chomp.split("")
end
visited = Array.new(h){Array.new(w,-1)}
visited[c[0]-1][c[1]-1] = 0
queue = [[c[0]-1,c[1]-1]]
queue2 = []
cnt = 0
map1 = [[-1,0],[1,0],[0,1],[0,-1]]
map2 = [[-2,-2],[-2,-1],[-2,0],[-2,1],[-2,2],[-1,-2],[-1,-1],[-1,1],[-1,2],[0,-2],[0,2],[1,-2],[1,-1],[1,1],[1,2],[2,-2],[2,-1],[2,0],[2,1],[2,2]]
while !queue.empty? || !queue2.empty?
  while !queue.empty?
    v = queue[0]
    queue.shift
    queue2 << v
    map1.each do |x|
      v0,v1 = v[0]+x[0],v[1]+x[1]
      if 0 <= v0 && v0 < h && 0 <= v1 && v1 < w
        if visited[v0][v1] == -1 && s[v0][v1] == "."
          visited[v0][v1] = cnt
          queue << [v0,v1]
        end
      end
    end
  end
  cnt += 1
  while !queue2.empty?
    v = queue2[0]
    queue2.shift
    map2.each do |x|
      v0,v1 = v[0]+x[0],v[1]+x[1]
      if 0 <= v0 && v0 < h && 0 <= v1 && v1 < w
        if visited[v0][v1] == -1 && s[v0][v1] == "."
          visited[v0][v1] = cnt
          queue << [v0,v1]
        end
      end
    end
  end
end
puts visited[d[0]-1][d[1]-1]
