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+2){Array.new}
h.times do |i|
  s[i] = gets.chomp.split("") + ["#","#"]
end
dis = Array.new(h){Array.new(w,-1)}
queue = [c[0]-1,c[1]-1,0]
while !queue.empty?
  x,y,v = queue[0],queue[1],queue[2]
  queue.shift
  queue.shift
  queue.shift
  if dis[x][y] == -1 || dis[x][y] > v
    dis[x][y] = v
    for i in -2..2
      for j in -2..2
        dx,dy = x+i,y+j
        if 0 <= dx && dx < h && 0 <= dy && dy < w && s[dx][dy] == "."
          if i.abs + j.abs == 1
            queue.unshift(v)
            queue.unshift(dy)
            queue.unshift(dx)
          else
            queue << dx
            queue << dy
            queue << v+1
          end
        end
      end
    end
  end
end
puts dis[d[0]-1][d[1]-1]