#include <iostream>
using namespace std;
int main(){
unsigned long in_val, tmp_val, power = 1;
cin >> in_val;
tmp_val = in_val;
while (tmp_val) {
tmp_val /= 2;
power *= 2;
}
tmp_val = in_val;
unsigned long max_val = in_val;
do {
in_val = in_val << 1;
in_val = in_val % power + (in_val >= power? 1: 0);
if (max_val < in_val) max_val = in_val;
} while (tmp_val != in_val );
cout << max_val << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKXsKCXVuc2lnbmVkIGxvbmcgaW5fdmFsLCB0bXBfdmFsLCBwb3dlciA9IDE7CgljaW4gPj4gaW5fdmFsOwoJdG1wX3ZhbCA9IGluX3ZhbDsKCXdoaWxlICh0bXBfdmFsKSB7CgkJdG1wX3ZhbCAvPSAyOwoJCXBvd2VyICo9IDI7Cgl9Cgl0bXBfdmFsID0gaW5fdmFsOwoJdW5zaWduZWQgbG9uZyBtYXhfdmFsID0gaW5fdmFsOwoJZG8gewoJCWluX3ZhbCA9IGluX3ZhbCA8PCAxOwoJCWluX3ZhbCA9IGluX3ZhbCAlIHBvd2VyICsgKGluX3ZhbCA+PSBwb3dlcj8gMTogMCk7CgkJaWYgKG1heF92YWwgPCBpbl92YWwpIG1heF92YWwgPSBpbl92YWw7Cgl9IHdoaWxlICh0bXBfdmFsICE9IGluX3ZhbCApOwoJY291dCA8PCBtYXhfdmFsIDw8IGVuZGw7CgkKCXJldHVybiAwOwp9