#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; auto range(int n) { return views::iota(0, n); } template ostream& operator<<(ostream& os, const pair& p){ return os << "{" << p.first << ", " << p.second << "}"; } template ostream& operator<<(ostream& os, const vector& obj) { os << "{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } template ostream& operator<<(ostream& os, const set& obj) { os << "set{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } template ostream& operator<<(ostream& os, const map& obj) { os << "map{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } #ifdef ONLINE_JUDGE #define dump(expr) ; #else #define dump(expr) { cerr << "\033[33m#L" << __LINE__ << ": " << expr << "\033[39m" << endl; } #endif ll solve() { ll y; cin >> y; ll res = y; for (ll a = 1; a <= y; a = 2 * a + 1) { ll rem = y % (2 * a + 1); ll tot = 0; for (ll b = a; b; b >>= 1) { if (rem >= b) { rem -= b; tot += (b + 1) >> 1; } } if (rem == 0) { ll x = y / (2 * a + 1); dump(a << " " << tot << " " << x); res = min(res, tot + (a + 1) * x); } } return res; } int main() { cout << solve() << endl; }