END { load __FILE__ unless $stdin.eof? }

h, w = gets.split.map! { |s| s.to_i + 4 }
c = gets.then { |s| j, i = s.split.map!(&:to_i); (j + 1) * w + i + 1 }
d = gets.then { |s| j, i = s.split.map!(&:to_i); (j + 1) * w + i + 1 }

maze = Array.new(h - 4) do
  "###{gets.chomp}##".chars.map! { |c| c == '.' ? 99999999 : -1 }
end
wall = Array.new(w, -1)
maze.unshift(wall, wall).push(wall, wall)
maze.flatten!

pq = Hash.new { |h, k| h[k] = [] }
pq[0] << c

until pq.empty?
  k = pq.keys.min
  q = pq[k]
  q.uniq!

  while (pos = q.pop)
    next if maze[pos] <= k
    maze[pos] = k
  
    [pos - w, pos - 1, pos + 1, pos + w].each do |x|
      if k < maze[x]
        q << x
      end
    end

    k2 = k + 1
    q2 = pq[k2]
    (-2..2).each do |j|
      (-2..2).each do |i|
        x = pos + (j * w + i)
        if k2 < maze[x]
          q2 << x
        end
      end
    end
  end

  pq.delete(k)
end

cnt = maze[d]
puts (cnt < 99999999) ? cnt : -1
