#include //#include //using namespace atcoder; using namespace std; #pragma region Macros using ll = long long; #define int ll using pii = pair; using tiii = tuple; template using V = vector; template using VV = V>; #define IOS\ ios::sync_with_stdio(false);\ cin.tie(0);\ cout.tie(0); #define FOR(i,l,r) for(int i=(l);i=int(r);--i) #define RREP(i,n) RFOR(i,n-1,0) #define RREPS(i,n) RFOR(i,n,1) #define mp make_pair #define mt make_tuple #define pb push_back #define eb emplace_back #define all(x) (x).begin(),(x).end() #define SORT(name) sort(name.begin(), name.end()) #define RSORT(name)\ SORT(name);\ reverse(all(name)); #define ZERO(p) memset(p, 0, sizeof(p)) #define MINUS(p) memset(p, -1, sizeof(p)) inline void Yes(bool b = true) {cout << (b ? "Yes" : "No") << '\n';} inline void YES(bool b = true) {cout << (b ? "YES" : "NO") << '\n';} template inline void print(T x){ cout << x << '\n';} template inline void chmin(T1 &a, T2 b){ if(a > b) a = b; } template inline void chmax(T1 &a, T2 b){ if(a < b) a = b; } const ll LLINF = (1LL<<60); const int INF = (1LL<<30); const double DINF = std::numeric_limits::infinity(); #pragma endregion const int MOD = 1000000007; #if 1 # define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__) #else # define DBG(fmt, ...) #endif const int MAX_N = 100010; int D; bool Check(int x) { int cur = 0; while(x > 0) { cur += x; if(cur == D) { return true; } x /= 2; } return false; } signed main() { IOS; cin >> D; V<> j_min, j_max; j_min.pb(1); j_max.pb(1); while(j_max[j_max.size()-1] < LLINF) { j_min.pb(j_min[j_min.size()-1] * 2); j_max.pb(j_max[j_max.size()-1] * 2 + 1); if(j_max.size() > 1 && j_max[j_max.size()-1] <= j_max[j_max.size()-2]) { j_max.pop_back(); j_min.pop_back(); break; } } V<> sum_min(j_min.size()), sum_max(j_max.size()); sum_min[0] = 1; sum_max[0] = 1; for(int i = 1; i < j_min.size(); ++i) { sum_min[i] = sum_min[i-1] + j_min[i]; sum_max[i] = sum_max[i-1] + j_max[i]; } // D が何回はねた時作れる可能性があるか int cnt = 0; REP(i, sum_min.size()) { if(sum_min[i] <= D && D <= sum_max[i]) { cnt = i; break; } } if(cnt == -1) { print(D); return 0; } int lb = sum_min[cnt], ub = sum_max[cnt]; //DBG("lb: %lld ub: %lld\n", lb, ub); int cur = 1; REP(i, cnt) { int mid = (ub + lb) / 2; if(mid < D) { lb = mid; cur = cur * 2 + 1; } else { ub = mid; cur = cur * 2; } } //DBG("cur: %lld\n", cur); // 最終 lb +-100 あたりに答えありそう for(int cand = cur - 100; cand <= cur + 100; ++cand) { if(cand <= 0 || cand > D) { continue; } if(Check(cand)) { print(cand); return 0; } } print(D); return 0; }