DEBUG = 0
INF = 10**9

h, w = gets.split.map &:to_i
ch, cw = gets.split.map &:to_i
dh, dw = gets.split.map &:to_i
# 番兵をつけておく
v_margin = [[0] * (w + 4)] * 2
dists = v_margin + h.times.map { [0, 0] + gets.chomp.each_char.map { _1 == '.' ? INF : 0 } + [0, 0] } + v_margin

puts maze if DEBUG >= 1

# 01-BFS
start = [ch - 1 + 2, cw - 1 + 2, 0]
dists[start[0]][start[1]] = 0
queue = [start]
until queue.empty?
  p queue if DEBUG >= 1
  puts maze if DEBUG >= 2
  i, j, cost = *queue.shift
  
  # ゴールなら
  if i == dh - 1 + 2 and j == dw - 1 + 2
    p cost
    exit
  end
  
  5.times do |di|
    di -= 2
    5.times do |dj|
      dj -= 2
      _i, _j, _cost = i + di, j + dj, cost + (di.abs + dj.abs == 1 ? 0 : 1)
      # 壁もしくは更新できない場合は除外
      next if _cost >= dists[_i][_j]
      dists[_i][_j] = _cost

      p [_i, _j, cost, di, dj] if DEBUG >= 2
      
      if di.abs + dj.abs == 1
        # 上下左右 -> 徒歩
        queue.unshift [_i, _j, cost]
      else
        # それ以外 -> ワープ
        queue.push [_i, _j, cost + 1]
      end
    end
  end
end

# 到達できない場合
p -1