h, w = gets.split(' ').map(&:to_i)
wi, wj = gets.split(' ').map(&:to_i)
di, dj = gets.split(' ').map(&:to_i)

wi -= 1
wj -= 1
di -= 1
dj -= 1

m = h.times.map { gets.chomp! }

DS = [[-1, 0], [1, 0], [0, -1], [0, 1]]

queue = [[wi, wj, 0]]
seen = { wi => { wj => true } }
until queue.empty?
  ci, cj, cd = queue.shift
  if ci == di && cj == dj
    puts cd
    exit
  end

  adjs = [[ci, cj]]
  
  adj_queue = [[ci, cj]]
  until adj_queue.empty?
    ai, aj = adj_queue.shift
    if ai == di && aj == dj
      puts cd
      exit
    end
    DS.each do |dir|
      ni = ai + dir[0]
      nj = aj + dir[1]
      next if (! ((0 <= ni && ni < h) && (0 <= nj && nj < w))) || m[ni][nj] == '#'
      seen[ni] ||= {}
      next if seen[ni][nj]
      seen[ni][nj] = true
      adj_queue.push([ni, nj])
      adjs.push([ni, nj])
    end
  end
p adjs

  adjs.each do |ai, aj|
    (-2..2).each do |pi|
      (-2..2).each do |pj|
        ni = ai + pi
        nj = aj + pj
        next if ni == ai && nj == ai
        next if (! ((0 <= ni && ni < h) && (0 <= nj && nj < w))) || m[ni][nj] == '#'
        seen[ni] ||= {}
        next if seen[ni][nj]
        seen[ni][nj] = true
        queue.push([ni, nj, cd + 1])
      end
    end
  end
  
end

puts -1
