(H, W), (Ch, Cw), (Dh, Dw) = 3.times.map{gets.split.map(&:to_i)}
Ss = [""] + readlines.map{" " + _1.strip}  # 1-indexed

mv = [[1, 0], [-1, 0], [0, 1], [0, -1]]
wp = [*-2 .. 2].repeated_permutation(2).to_a - [[0, 0]] - mv
q1, q2, c = [Ch, Cw], [], 0
d = Array.new(H + 1){Array.new(W + 1, -1)}
until q1.empty? && q2.empty?
  while !q1.empty?
    h, w = q1.shift(2)
    q2.append(h, w)
    mv.map{[_1 + h, _2 + w]}.each do |x, y|
      next if x < 1 || H < x || y < 1 || W < y || Ss[x][y] == '#' || d[x][y] >= 0
      d[x][y] = c
      q1.append(x, y)
    end
  end
  c += 1
  while !q2.empty?
    h, w = q2.shift(2)
    wp.map{[_1 + h, _2 + w]}.each do |x, y|
      next if x < 1 || H < x || y < 1 || W < y || Ss[x][y] == '#' || d[x][y] >= 0
      d[x][y] = c
      q1.append(x, y)
    end
  end
end
puts d[Dh][Dw]