# after YouTube
dh = [-1,0,1,0]
dw = [0,-1,0,1]
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 = []
h.times { s.push gets.strip }
INF = 1_000_000_000
dt = Array.new(h){Array.new(w,INF)}
dt[c[0]][c[1]] = 0
q = [[c[0],c[1]]]
while !q.empty?
  hh,ww = q.shift
  dd = dt[hh][ww]
  4.times do |v|
    nh = hh+dh[v]
    nw = ww+dw[v]
    next if nh <0 || nh >= h || nw <0 || nw >= w
    next if s[nh][nw] == '#'
    next if dt[nh][nw] <= dd
    dt[nh][nw] = dd
    q.unshift [nh, nw]
  end
  -2.upto(2) do |eh|
    -2.upto(2) do |ew|
      nh = hh + eh
      nw = ww + ew
      next if nh <0 || nh >= h || nw <0 || nw >= w
      next if s[nh][nw] == '#'
      next if dt[nh][nw] <= dd+1
      dt[nh][nw] = dd+1
      q.push [nh, nw]
    end
  end
end
ans = dt[d[0]][d[1]]
if ans == INF
  p -1
else
  p ans
end