Envious Exponents

Given N and k, find the smallest integer greater than N whose binary representation has exactly k ones.

Medium7Bit manipulationGreedyMathImplementationInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

Alice and Bob own an integer NN. Neither of them is happy with it. Last night they went to a cocktail party and found out that another couple had the exact same integer, so they are getting a new one.

Bob wants to impress the other couple, so he thinks the new integer has to be strictly larger than NN.

Alice is fond of one specific integer kk. She therefore thinks that whatever integer they pick, it must be possible to write it as a sum of kk distinct powers of 2.

Bob is also a cheapskate and wants to spend as little money as possible. The price of an integer grows with its size, so he wants the smallest integer that satisfies both conditions.

Input

The first line contains two integers NN and kk separated by a space. (1N10181 \le N \le 10^{18}, 1k601 \le k \le 60)

Output

Print MM, the smallest integer larger than NN that can be written as the sum of exactly kk distinct powers of 2.