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]]

visited = { wi => { wj => 0 } }
queue = [[wi, wj, 0]]
until queue.empty?
  ci, cj, cd = queue.shift
  DS.each do |dir|
    ni = ci + dir[0]
    nj = cj + dir[1]
    next if ! ((0 <= ni && ni < h) && (0 <= nj && nj < w))
    next if m[ni][nj] == '#'
    visited[ni] ||= {}
    next if visited[ni][nj] && visited[ni][nj] <= cd
    visited[ni][nj] = cd
    queue.unshift([ni, nj, cd])
  end

  (-2..2).each do |pi|
    (-2..2).each do |pj|
      manh = pi.abs + pj.abs
      next if manh <= 1
      ni = ci + pi
      nj = cj + pj
      next if ! ((0 <= ni && ni < h) && (0 <= nj && nj < w))
      next if m[ni][nj] == '#'
      visited[ni] ||= {}
      next if visited[ni][nj]
      visited[ni][nj] = cd + 1
      queue.push([ni, nj, cd + 1])
    end
  end
end

visited[di] ||= {}
puts visited[di][dj] || -1
