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
map1x = [-1,1,0,0]
map1y = [0,0,1,-1]
map2x = [-2,-2,-2,-2,-2,-1,-1,-1,-1,0,0,1,1,1,1,2,2,2,2,2,2]
map2y = [-2,-1,0,1,2,-2,-1,1,2,-2,2,-2,-1,1,2,-2,-1,0,1,2]
while !queue.empty? || !queue2.empty?
  while !queue.empty?
    a,b = queue[0],queue[1]
    queue.shift
    queue.shift
    queue2 << a
    queue2 << b
    4.times do |i|
      v0,v1 = a+map1x[i],b+map1y[i]
      if 0 <= v0 && v0 < h && 0 <= v1 && v1 < w
        if visited[v0][v1] == -1 && s[v0][v1] == "."
          visited[v0][v1] = cnt
          queue << v0
          queue << v1
        end
      end
    end
  end
  cnt += 1
  while !queue2.empty?
    a = queue2[0]
    b = queue2[1]
    queue2.shift
    queue2.shift
    20.times do |i|
      v0,v1 = a+map2x[i],b+map2y[i]
      if 0 <= v0 && v0 < h && 0 <= v1 && v1 < w
        if visited[v0][v1] == -1 && s[v0][v1] == "."
          visited[v0][v1] = cnt
          queue << v0
            queue << v1
        end
      end
    end
  end
end
puts visited[d[0]-1][d[1]-1]