class Problem
  attr_accessor *(?a..?z).to_a.map(&:to_sym)
  attr_accessor :map, :history
  def initialize
    @h,@w = gets.split.map(&:to_i)
    @c = gets.split.map(&:to_i).map{ _1 - 1 }
    @d = gets.split.map(&:to_i).map{ _1 - 1 }
    @map = Array.new(h){ gets.chomp }
    @history = Array.new(h){ Array.new(w,0) }
  end

  def eachgrid(y, x)
    yield y + 1, x if y < h - 1
    yield y - 1, x if y > 0
    yield y , x + 1 if x < w - 1
    yield y , x - 1 if x > 0
  end

  def each_warp(y,x)
    (y-2).upto(y+2) do |ny|
      (x-2).upto(x+2) do |nx|
        next if ny == y && nx == x
        next if nx < 0
        next if ny < 0
        next if h <= ny
        next if w <= nx
        next if map[ny][nx] == "#"
        yield ny,nx
      end
    end
  end

  def set_area_number
    seen = Array.new(h){ Array.new(w,false) }
    id = 0
    h.times do |y|
      w.times do |x|
        next if seen[y][x]
        seen[y][x] = true
        next if map[y][x] == "#"
        id += 1
        history[y][x] = id
        que = [[y,x]]
        while que.size > 0
          y,x = que.shift
          eachgrid(y,x) do |ny,nx|
            next if seen[ny][nx]
            seen[ny][nx] = true
            next if map[ny][nx] == "#"
            history[ny][nx] = id
            que << [ny,nx]
          end
        end
      end
    end
  end

  def solve
    seen = Array.new(h){ Array.new(w,false) }
    cy,cx = c
    dy,dx = d
    id = history[cy][cx]
    que = [[cy,cx, [id]]]
    seen[cy][cx] = true
    while que.size > 0
      y,x,ids = que.shift
      return ids if y == dy && x == dx
      each_warp(y,x) do |ny,nx|
        next if seen[ny][nx]
        seen[ny][nx] = true
        nids = ids | [history[ny][nx]]
        que << [ny,nx,nids]
      end
    end
    return -1
  end

  def show(ans)
    puts ans == -1 ? -1 : ans.size - 1
  end
end

Problem.new.instance_eval do
  set_area_number
  show(solve)
  # pp self
end