H,W = gets.split.map(&:to_i)
SY,SX = gets.split.map { _1.to_i - 1 }
GY,GX = gets.split.map { _1.to_i - 1 }
S = readlines.map {|s| s.chomp.chars }
A = Array.new(H) { [0]*W }

def mv(y,x,n)
  return if S.dig(y, x) == nil
  return if A[y][x] != 0 && A[y][x] <= n
  if S[y][x] == "#"
    A[y][x] = -1 
    n += 1
  else
    A[y][x] = n
  end
  mv(y+1,x,n)
  mv(y-1,x,n)
  mv(y,x+1,n)
  mv(y,x+1,n)
end
mv(SY, SX, 1)
puts A[GY][GX] - 1
