H,W,Ch,Cw,Dh,Dw = $<.take(3).join.split.map &:to_i
M = [-(?#*(W+2))].fill(1,H) { "##{gets.chomp}#" }
M << M[0] << 0
N = [[1,0],[0,1],[-1,0],[0,-1]]
P = [-2,-1,1,2].repeated_permutation(2)
Q = [[Ch,Cw]]
R = []
p (0..).each {|n|
  R.pop(H*W).each {|h,w|
    P.each {|r,c| M[r+=h][c+=w] == ?. and Q << [r,c] }
  }
  while Q[0]
    Q.pop(H*W).each {|h,w|
      next  if M[h][w] != ?.
      M[h][w] = ?*
      N.each{|r,c|
        m = M[r+=h][c+=w]
        if m == ?.
          Q << [r,c]
        elsif m == ?#
          R << [h,w]
        end
      }
    }
  end
  break n if M[Dh][Dw] != ?.
  break -1 if !R[0]
}