
countDiv n p = inner n 0 0
  where inner m k 0 = inner (div m p) (succ k) (mod m p)
        inner m k 1 = pred k

main = interact $ (++ "\n") . show . minimum
  . map ((`countDiv` 2) . read) . readInput . lines
  where readInput (x:y:_) = take (read x) (words y)
