/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package easy;
import java.io.BufferedInputStream;
import java.util.Scanner;
/**
*
* @author NC
*/
public class Poj2453 {
public static void main(String[] args) {
Scanner scan = new Scanner(new BufferedInputStream(System.in));
while (scan.hasNext()) {
int n = scan.nextInt();
if (n == 0) {
break;
}
int count = 0;
char[] s = Integer.toBinaryString(n).toCharArray();
for (int i = 0; i < s.length; i++) {
if (s[i] == '1') {
count++;
}
}
int t = 0;
while (count != t) {
n++;
char[] ss = Integer.toBinaryString(n).toCharArray();
t = 0;
for (int i = 0; i < ss.length; i++) {
if (ss[i] == '1') {
t++;
}
}
}
System.out.println(n);
}
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package easy;
import java.io.BufferedInputStream;
import java.util.Scanner;
/**
*
* @author NC
*/
public class Poj2453 {
public static void main(String[] args) {
Scanner scan = new Scanner(new BufferedInputStream(System.in));
while (scan.hasNext()) {
int n = scan.nextInt();
if (n == 0) {
break;
}
int count = 0;
char[] s = Integer.toBinaryString(n).toCharArray();
for (int i = 0; i < s.length; i++) {
if (s[i] == '1') {
count++;
}
}
int t = 0;
while (count != t) {
n++;
char[] ss = Integer.toBinaryString(n).toCharArray();
t = 0;
for (int i = 0; i < ss.length; i++) {
if (ss[i] == '1') {
t++;
}
}
}
System.out.println(n);
}
}
}