def idou(ans, s, x, y, i)
  ans[x][y] = i
  idou_n = 0
  (-1..1).each do |j|
    (-1..1).each do |k|
      if j.abs + k.abs == 2
        next
      elsif x+j >= 0 && x+j < s.length && y+k >= 0 && y+k < s[x].length && s[x+j][y+k] != "#" && (ans[x+j][y+k].nil? || i < ans[x+j][y+k])
        idou(ans, s, x+j, y+k, i)
        idou_n += 1
      end
    end
  end
  if idou_n > 0
    return
  end
  (-2..2).each do |j|
    (-2..2).each do |k|
      if (j == 0 && k.abs <= 1) || (k == 0 && j.abs <= 1)
        next
      else
        idou(ans, s, x+j, y+k, i+1) if x+j >= 0 && x+j < s.length && y+k >= 0 && y+k < s[x].length && s[x+j][y+k] != "#" && (ans[x+j][y+k].nil? || i < ans[x+j][y+k])
      end
    end
  end
end

h, w = gets.split.map(&:to_i)
ch, cw = gets.split.map{|s| s.to_i - 1}
dh, dw = gets.split.map{|s| s.to_i - 1}

s_hw = h.times.map{|_| gets.chomp.split("")}
ans_hw = Array.new(h).map{|_| Array.new(w)}

idou(ans_hw, s_hw, ch, cw, 0)
if ans_hw[dh][dw].nil?
  puts "-1"
else
  p ans_hw[dh][dw]
end