H, W = gets.chomp.split(" ").map(&:to_i)
C = gets.chomp.split(" ").map(&:to_i)
D = gets.chomp.split(" ").map(&:to_i)
S = [Array.new(W+4, -2), Array.new(W+4, -2)]
H.times{
  S << [-2, -2] + gets.chomp.chars.map{|s| s=='#' ? -2 : -1} + [-2, -2]
}
S << Array.new(W+4, -2)
S << Array.new(W+4, -2)

def add_lst(c, r, stp, lst)
  (S[c][r] = stp; lst << [c, r]) if S[c][r] == -1
end

WP = (-2..2).to_a.product((-2..2).to_a).select{|c, r| c.abs+r.abs > 1}
idx,stp, nxt = 0, 0, []
add_lst(C[0]+1, C[1]+1, stp, nxt)

until nxt.empty? do
  while nxt.size > idx do
    c, r = nxt[idx]
    add_lst(c, r+1, stp, nxt)
    add_lst(c, r-1, stp, nxt)
    add_lst(c+1, r, stp, nxt)
    add_lst(c-1, r, stp, nxt)
    idx += 1
  end
  wrp = []
  stp += 1
  nxt.each{|c, r|
    WP.each{|i, j| add_lst(c+i, r+j, stp, wrp)}
  }
  idx, nxt = 0, wrp
end

puts S[D[0]+1][D[1]+1]
