cho.sh
Notes
Loading...

Water Bottles

Time limit

1s

Memory limit

512 MB

Problem

Jimin has N bottles, each initially containing 1 liter of water. Any bottle can hold an unlimited amount of water. Jimin wants to move the bottles to another place, but he can carry at most K bottles at once and wants to make only one trip. Without wasting water, he wants to merge the water so that at most K non-empty bottles remain.

Water may be merged only by this rule.

Choose two bottles that contain the same amount of water, then pour all water from one bottle into the other. Repeat this as many times as needed.

It may be impossible to reduce the original N bottles to at most K non-empty bottles. In that case, Jimin may buy additional bottles from a store; each purchased bottle contains exactly 1 liter of water. Find the minimum number of bottles he must buy.

When N=3 and K=1, the original three bottles cannot become one bottle. Merging two 1-liter bottles leaves one 2-liter bottle and one 1-liter bottle. If he buys one more bottle, he can make another 2-liter bottle and then merge the two 2-liter bottles into one 4-liter bottle.

Input

The first line contains two natural numbers N and K separated by a space.

  • 1 <= N <= 10,000,000
  • 1 <= K <= 1,000

Output

Print the minimum number of bottles that must be bought from the store. If there is no answer, print -1.