h, w = gets.chomp.split(' ').map(&:to_i)
ch, cw = gets.chomp.split(' ').map { |i| i.to_i - 1 }
dh, dw = gets.chomp.split(' ').map { |i| i.to_i - 1 }
field = []
h.times { field << gets.chomp.split('').map { |i| i == '.' ? -1 : -2 } }

vec = [[1, 0], [0, 1], [-1, 0], [0, -1]]
queue, cur = [[ch, cw]], 0
until queue.empty?
  boarder_list = []
  until queue.empty?
    ph, pw = queue.shift
    field[ph][pw] = cur
    vec.each do |vh, vw|
      nph, npw = vh + ph, vw + pw
      next if nph >= h || nph < 0 || npw >= w || npw < 0
      if field[nph][npw] == -1
        field[ph][pw] = cur
        queue << [nph, npw]
      else
        boarder_list << [ph, pw]
      end
    end
  end
  cur += 1
  boarder_list.each do |lh, lw|
    (-2..2).each do |hh|
      (-2..2).each do |ww|
        nph, npw = lh + hh, lw + ww
        next if (hh == 0 && ww == 0) || nph >= h || nph < 0 || npw >= w || npw < 0 || field[nph][npw] != -1
        field[nph][npw] = cur
        queue << [nph, npw]
      end
    end
  end
end
puts field[dh][dw] < 0 ? -1 : field[dh][dw]
