h, w = gets.split.map(&:to_i)
x, y = gets.split.map(&:to_i)
d_h, d_w = gets.split.map(&:to_i)
x -= 1
y -= 1
d_h -= 1
d_w -= 1
INF = 10**9
dist = Array.new(h).map { Array.new(w, INF) }
s = []
h.times { s << gets.chomp.split('') }
nnx = [0, 1, 0, -1]
nny = [-1, 0, 1, 0]
queue = []
dist[x][y] = 0
queue.unshift([x, y, 0])
until queue.empty?
  q = queue.shift
  d = q[2]
  4.times do |i|
    nni = q[0] + nnx[i]
    nnj = q[1] + nny[i]
    next if nni < 0 || nnj < 0 || nni >= h || nnj >= w
    next if s[nni][nnj] == '#'
    next if dist[nni][nnj] <= d

    dist[nni][nnj] = d
    queue.unshift([nni, nnj, d])
  end
  [*-2..2].each do |magic_i|
    [*-2..2].each do |magic_j|
      nni = q[0] + magic_i
      nnj = q[1] + magic_j
      next if nni < 0 || nnj < 0 || nni >= h || nnj >= w
      next if s[nni][nnj] == '#'
      next if dist[nni][nnj] <= d+1

      dist[nni][nnj] = d+1
      queue.unshift([nni, nnj, d+1])

    end
  end
end

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