L=100000000
h,w=gets.split.map &:to_i
ch,cw=gets.split.map &:to_i
dh,dw=gets.split.map &:to_i
cp=(w+4)*(ch+1)+cw+1
dp=(w+4)*(dh+1)+dw+1
d=w+4
f=[nil]*( (h+4)*(w+4) )
h.times{|i|
  gets.chomp.each_char.with_index{|c,j|
    next if c==?#
    f[d*(i+2)+j+2]=L
  }
}
qc=[cp]
qn=[]
dns=[-d,-1,1,d]
dms=(-2..2).flat_map{|i|
  (-2..2).each_with_object([]){|j,r|
    e=i*d+j
    r.push(e) if e.abs>1 && e.abs!=d
  }
}
#p [cp,dp,dns,dms,f]
0.step{|x|
  break if qc.empty? && qn.empty?
  until qc.empty?
    e=qc.pop
    next if f[e]<=x
    f[e]=x
    dns.each{|y|
      qc.push(e+y) if f[e+y]
    }
    dms.each{|y|
      qn.push(e+y) if f[e+y]
    }
  end
  qc=qn
  qn=[]
}
puts f[dp]==L ? -1 : f[dp]
