#include <iostream>
#include <math.h>
using namespace std;
int main(){
unsigned long in_val, tmp_val;
cin >> in_val;
tmp_val = in_val;
unsigned long power;
power = pow(2, floor(log2(in_val)) + 1);
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;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWF0aC5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKXsKCXVuc2lnbmVkIGxvbmcgaW5fdmFsLCB0bXBfdmFsOwoJY2luID4+IGluX3ZhbDsKCXRtcF92YWwgPSBpbl92YWw7Cgl1bnNpZ25lZCBsb25nIHBvd2VyOwoJcG93ZXIgPSBwb3coMiwgZmxvb3IobG9nMihpbl92YWwpKSArIDEpOwoJdG1wX3ZhbCA9IGluX3ZhbDsKCXVuc2lnbmVkIGxvbmcgbWF4X3ZhbCA9IGluX3ZhbDsKCWRvewoJCWluX3ZhbCA9IGluX3ZhbCA8PCAxOwoJCWluX3ZhbCA9IGluX3ZhbCAlIHBvd2VyICsgKGluX3ZhbCA+PSBwb3dlciA/IDEgOiAwKTsKCQlpZihtYXhfdmFsIDwgaW5fdmFsKXsKCQkJbWF4X3ZhbCA9IGluX3ZhbDsKCQl9Cgl9d2hpbGUodG1wX3ZhbCAhPSBpbl92YWwpOwoJY291dCA8PCBtYXhfdmFsIDw8IGVuZGw7CgoJcmV0dXJuIDA7Cn0=