#t369. 蒟蒻的问题

蒟蒻的问题

The Next Integer with the Same Number of 1s

Problem Description

Given a positive integer NN, find the smallest positive integer MM greater than NN such that the binary representations of MM and NN contain the same number of 11s.

For example, given N=78N=78, its binary representation is 10011101001110, which contains 44 ones. The smallest number greater than NN whose binary representation also contains exactly 44 ones is 8383 (binary 10100111010011), so 8383 is the answer.

Input Format

There are several lines of input. Each line contains one integer n (1≤n≤1000000)n\ (1\le n\le 1000000). Input ends with 0.

Output Format

For each input line, output the corresponding answer on its own line.

Sample Input

1
2
3
4
78
0

Sample Output

2
4
5
8
83

Hint

Source: ybt (一本通 online judge).