### SNIPPET
  # n = gets.split.map(&:to_i)
  # array = n.times.map { gets.split.map(&:to_i) }
  # [].all?(&:even?)
  # a = [*1..m].repeated_combination(n).to_a
  # [1,2,3,4,5].select { |num| num.even? }  # => [2, 4]
  # ["a","a","b"].group_by(&:itself).map{|k,v| [k, v.count]}.to_h #=> {"a"=>2, "b"=>1}
  # 切り捨て: .floor(2).to_f ,切り上げ： .ceil(2).to_f,四捨五入: round(2)
  # 3.upto(6) do |i|, 6.downto(3) do |i|
  # 公約数125.gcd(100)、公倍数125.lcm(100)
  # PI = Math::PI
  # 高さ = a * Math.sin(w / 180.0 * Math::PI), 底辺 = a * Math.cos(w / 180.0 * Math::PI)
  # def chmax(a, b) a > b ? a : b end
  # INF = Float::INFINITY
  def chmin(a, b) a < b ? a : b end

# require 'pry'

h,w = gets.split.map(&:to_i)
ci,cj = gets.split.map(&:to_i)
di,dj = gets.split.map(&:to_i)

ci -= 1
cj -= 1
di -= 1
dj -= 1

s = []
h.times do |_|
  s << gets.chomp.split("")
end

s[ci][cj] = 0

dequeue = []
dequeue.unshift([ci,cj])

ki = [0,1,0,-1] # 右下左上
kj = [1,0,-1,0] # 右下左上
while dequeue.length != 0
  target = dequeue.shift
  4.times do |i|
    if (target[0] + ki[i]) >= 0 && (target[0] + ki[i]) < h && (target[1] + kj[i]) >= 0 && (target[1] + kj[i]) < w
      next_pos = s[target[0] + ki[i]][target[1]+kj[i]] 
      if next_pos != '#'
        if next_pos == '.'
          s[target[0] + ki[i]][target[1]+kj[i]] = s[target[0]][target[1]]
          dequeue.unshift([(target[0] + ki[i]), (target[1]+kj[i])])
        else
          s[target[0] + ki[i]][target[1]+kj[i]] = chmin(s[target[0]][target[1]], s[target[0] + ki[i]][target[1]+kj[i]])
        end
      end
    end
  end

  [-2,-1,0,1,2].each do |i|
    [-2,-1,0,1,2].each do |j|
      if (target[0] + i) >= 0 && (target[0] + i) < h && (target[1] + j) >= 0 && (target[1] + j) < w
        next_pos = s[target[0] + i][target[1]+j]
        if next_pos == '.'
          s[target[0] + i][target[1]+j] = s[target[0]][target[1]] + 1
          dequeue << [(target[0] + i),(target[1]+j)]
        end
      end
    end
  end

end

# pp s


puts s[di][dj] != "." ? s[di][dj] : "-1"