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

def list_adjacents(i, j)
  adjs = [[i, j]]
  adj_queue = [[i, j]]
  adj_visited = (1..$h).map { (1..$w).map { false } }
  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 adj_visited[ni][nj]
      adj_visited[ni][nj] = true
      adjs.push([ni, nj])
      adj_queue.unshift([ni, nj])
    end
  end
  adjs
end

visited = (1..$h).map { (1..$w).map { false } }
first_adjs = list_adjacents($wi, $wj)
first_adjs.each do |ai, aj|
  visited[ai][aj] = true
end
queue = [[first_adjs, 0]]

until queue.empty?
  adjs, cd = queue.shift
  adjs.each do |ai, aj|
    if ai == $di && aj == $dj
      puts cd
      exit
    end
    (-2..2).each do |pi|
      (-2..2).each do |pj|
        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]
        next_adjs = list_adjacents(ni, nj)
        next_adjs.each do |ai, aj|
          visited[ai][aj] = true
        end
        queue.push([next_adjs, cd + 1])
      end
    end
  end
end

puts -1
