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

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