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
    @m = id
  end

  def make_graph
    @r = Array.new(m+1){ [] }
    h.times do |y|
      w.times do |x|
        next if map[y][x] == "#"
        sid = history[y][x]
        each_warp(y,x) do |ny,nx|
          next if map[ny][nx] == "#"
          did = history[ny][nx]
          next if sid == did
          r[sid] << did unless r[sid].include?(did)
          r[did] << sid unless r[did].include?(sid)
        end
      end
    end
  end

  def solve
    cy,cx = c
    dy,dx = d
    ch = history[cy][cx]
    dh = history[dy][dx]
    return 0 if ch == dh
    seen = Array.new(m+1, false)
    seen[ch] = true
    que = [[ch,0]]
    while que.size > 0
      v,cnt = que.shift
      return cnt if v == dh
      r[v].each do |nv|
        next if seen[nv]
        seen[nv] = true
        que << [nv, cnt + 1]
      end
    end
    return -1
  end

  def show(ans)
    puts ans
  end
end

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