INF = 10**9
nn = [[0,-1], [1,0], [0,1], [-1, 0]]
warp_index = [*-2..2]

h, w = gets.split.map(&:to_i)
c_h, c_w = gets.split.map{ _1.to_i - 1}
d_h, d_w = gets.split.map{ _1.to_i - 1}
s = h.times.map { gets.chomp.chars }

dist = Array.new(h).map { Array.new(w, INF) }
dist[c_h][c_w] = 0
queue = [[c_h, c_w]]
d = 0
loop do
  warp_q = []
  while q = queue.shift
    x, y = q
    nn.each do |nnx, nny|
      nni = x + nnx
      nnj = y + nny
      next if nni < 0 || nnj < 0 || nni >= h || nnj >= w
      next if dist[nni][nnj] <= d
      if s[nni][nnj] == '#'
        warp_q << [x, y]
      else
        queue << [nni, nnj]
      end

      dist[nni][nnj] = d
    end
  end

  d += 1
  warp_q.uniq.each do |wx, wy|
    warp_index.each do |warp_i|
      nni = wx + warp_i
      next if nni < 0 || nni >= h
      warp_index.each do |warp_j|
        nnj = wy + warp_j
        next if nnj < 0 || nnj >= w
        next if dist[nni][nnj] <= d
        next if s[nni][nnj] == '#'

        dist[nni][nnj] = d
        queue << [nni, nnj]
      end
    end
  end
  queue.uniq!
  break if queue.empty?
end

d = dist[d_h][d_w]
puts d == INF ? '-1' : d