(H, W), (Ch, Cw), (Dh, Dw) = 3.times.map{gets.split.map(&:to_i)}
Ss = ['#'*(W + 3)] + readlines.map{'#' + _1.chomp + '##'} + ['#'*(W + 3)]*2
INF = 1 << 20

mv = [[1, 0], [-1, 0], [0, 1], [0, -1]]
wp = [*-2 .. 2].repeated_permutation(2).to_a - [[0, 0]] - mv
d = Array.new(H + 3){Array.new(W + 3, INF)}
d[Ch][Cw] = 0
q1 = [Ch, Cw]
(0 ..).each do |k|
  break if q1.empty? || d[Dh][Dw] < INF
  q2 = []
  while !q1.empty?
    h, w = q1.pop(2)
    mv.each do |a, b|
      x = h + a
      y = w + b
      next if Ss[x][y] == '#' || d[x][y] <= k
      d[x][y] = k
      q1.append(x, y)
    end
    wp.each do |a, b|
      x = h + a
      y = w + b
      next if Ss[x][y] == '#' || d[x][y] <= k + 1
      d[x][y] = k + 1
      q2.append(x, y)
    end
  end
  q1 = q2
end
puts d[Dh][Dw] == INF ? -1 : d[Dh][Dw]