h, w = gets.split.map(&:to_i)
c = gets.split.map(&:to_i)
d = gets.split.map(&:to_i)
s = Array.new(h + 4) { Array.new(w + 4, -1) }
h.times do |j|
  gets.chomp.chars.each_with_index do |t, i|
    if t == '.'
      s[j + 2][i + 2] = 0
    end
  end
end
que = []
que << [c[0] + 1, c[1] + 1]
s[c[0] + 1][c[1] + 1] = 1
while que.size > 0
  h = {}
  while que.size > 0
    y, x = que.shift
    cnt = s[y][x]
    if s[y + 1][x] == 0
      s[y + 1][x] = cnt
      que << [y + 1, x]
    elsif s[y + 1][x] == -1
      h[[y, x]] = 1
    end
    if s[y - 1][x] == 0
      s[y - 1][x] = cnt
      que << [y - 1, x]
    elsif s[y - 1][x] == -1
      h[[y, x]] = 1
    end
    if s[y][x + 1] == 0
      s[y][x + 1] = cnt
      que << [y, x + 1]
    elsif s[y][x + 1] == -1
      h[[y, x]] = 1
    end
    if s[y][x - 1] == 0
      s[y][x - 1] = cnt
      que << [y, x - 1]
    elsif s[y][x - 1] == -1
      h[[y, x]] = 1
    end
  end
  h.keys.each do |y, x|
    cnt = s[y][x]
    (y - 2).upto(y + 2) do |j|
      (x - 2).upto(x + 2) do |i|
        if s[j][i] == 0
          s[j][i] = cnt + 1
          que << [j, i]
        end
      end
    end
  end
end
puts s[d[0] + 1][d[1] + 1] - 1