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 = (1..h).map { Array.new(w, -1) }
visited[wi][wj] = 0
queue = [[wi, wj, 0]]
until queue.empty?
  ci, cj, cd = queue.shift
  adjs = [[ci, cj]]
  adj_queue = [[ci, cj]]
  until adj_queue.empty?
    ai, aj = adj_queue.shift
    DS.each do |dir|
      ni = ai + dir[0]
      nj = aj + dir[1]
      next if ! ((0 <= ni && ni < h) && (0 <= nj && nj < w))
      next if m[ni][nj] == '#'
      next if visited[ni][nj] != -1
      visited[ni][nj] = cd
      adjs.push([ni, nj])
      adj_queue.unshift([ni, nj])
    end
  end

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

puts visited[di][dj] || -1
