h, w = gets.split(' ').map(&:to_i)
wi, wj = gets.split(' ').map(&:to_i)
di, dj = gets.split(' ').map(&:to_i)

m = h.times.map { gets.chomp! }
gs = h.times.map { w.times.map { -1 } }


DS = [[-1, 0], [1, 0], [0, -1], [0, 1]]
MAX_VAL = 1 << 31

visited = {}
group_id = -1
(0...h).each do |i|
  (0...w).each do |j|
    next if m[i][j] == '#'
    visited[i] ||= {}
    next if visited[i][j]
    visited[i][j] = true
    stack = [[i, j]]
    group_id += 1
    until stack.empty?
      ci, cj = stack.pop
      gs[ci][cj] = group_id
      DS.each do |dir|
        ni = ci + dir[0]
        nj = cj + dir[1]
        visited[ni] ||= {}
        if ! visited[ni][nj] && (0 <= ni && ni < h) && (0 <= nj && nj < w) && m[ni][nj] == '.'
          visited[ni][nj] = true
          stack.push([ni, nj])
        end
      end
    end
  end
end

f = {}
edges = {}
(0...h).each do |i|
  (0...w).each do |j|
    next if gs[i][j] == -1
    (-2..2).each do |pi|
      (-2..2).each do |pj|
        ci = i + pi
        cj = j + pj
        next unless (0 <= ci && ci < h) && (0 <= cj && cj < w)
        next if gs[ci][cj] == -1
        g = gs[ci][cj]
        next if g == gs[i][j]
        f[ci] ||= {}
        f[ci][cj] ||= {}
        f[ci][cj].keys.each do |tg|
          if g != tg
            edges[g] ||= {}
            edges[tg] ||= {}
            edges[g][tg] = true
            edges[tg][g] = true
          end
        end
        f[ci][cj][gs[i][j]] = true
      end
    end
  end
end

start = gs[wi - 1][wj - 1]
dists = Array.new(group_id + 1, MAX_VAL)
dists[start] = 0
queue = [start]
seen = {}
until queue.empty?
  cur = queue.shift
  seen[cur] = true
  next unless edges[cur]
  edges[cur].keys.each do |nxt|
    unless seen[nxt]
      dists[nxt] = dists[cur] + 1 if dists[nxt] > dists[cur] + 1
      queue << nxt
    end
  end
end

ret = dists[gs[di - 1][dj - 1]]
puts ret == MAX_VAL ? -1 : ret





