# after editorial
h,w = gets.strip.split.map(&:to_i)
c = gets.strip.split.map{|e| e.to_i + 1}
d = gets.strip.split.map{|e| e.to_i + 1}
s = [Array.new(w+4,-1),Array.new(w+4,-1)]
h.times do
  t = [-1,-1]
  gets.strip.each_char do |e|
    case e
    when '#'
      t.push -1
    when '.'
      t.push 0
    end
  end
  t.push -1
  t.push -1
  s.push t
end
s.push Array.new(w+4,-1)
s.push Array.new(w+4,-1)
q = [[c[0],c[1]]]
ct = 1
qq = []
loop do
  while q.size > 0
    hh,ww = q.shift
    if s[hh][ww] == 0
      if hh == d[0] && ww == d[1]
        p ct - 1
        exit
      end
      s[hh][ww] = ct
      qq.push [hh,ww] if s[hh][ww+1] == -1 || s[hh][ww-1] == -1 || s[hh+1][ww] == -1 || s[hh-1][ww] == -1
      q.push [hh,ww+1]
      q.push [hh,ww-1]
      q.push [hh+1,ww]
      q.push [hh-1,ww]
    end
  end
  ct += 1
  while qq.size > 0
    hh,ww = qq.shift
    5.times do |i|
      5.times do |j|
        hi = hh+i-2
        wj = ww+j-2
        if s[hi][wj] == 0
          q.push [hi,wj]
        end
      end
    end
  end
  q.uniq!
  if q.size == 0
    p -1
    exit
  end
end