#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

ll N;

void input(void){ cin >> N; }

ll fix_b(int b){
    ll pow2b = (1LL << b);
    ll a_max = N / pow2b;
    ll c = N - pow2b * a_max;
    return (a_max + c + b);    
}

int main(void){
    input();
    ll ans = (1LL << 60);
    for (int b = 0; b <= 60; ++b)
        ans = min(ans, fix_b(b));
    cout << ans << endl;
    return 0;
}
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define task "talltree"
#define pll pair<ll, ll>
#define pii pair<pll, pll>
#define fi first
#define se second
#define ull unsigned long long

using namespace std;
const ll mod = 998244353;
const ll N = 1e5+5;

ll n, m, t, ans, k, a[N][22], b[N], c[N], tong,  cnt, q, d[N], dp[N];
struct point
{
    ll x, id;
    char s;
    bool operator < (const point& q)
    {
        return x < q.x;
    }
}p[N];
vector<pll> adj[N];

string s[N];
ll pw(ll k, ll n)
{
    ll total = 1;
    for(; n; n >>= 1)
    {
        if(n & 1)total = total * k % mod;
        k = k * k % mod;
    }
    return total;
}
ll A(ll u, ll v)
{
    if(u > v)return 0;
    return b[v] * c[v-u] % mod;
}
void sol()
{
    cin >> n;
    ans = n;
    while(n >= (1ll<<(t)))
    {
        ans = min(ans, n / (1ll<<t) + t + n % (1ll<<t));
        ++t;
    }
    cout << ans;

}
int main()
{
    if(fopen(task".inp", "r")){
       freopen(task".inp", "r", stdin);
       freopen(task".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int ntest = 1;
    //cin >> ntest;
    while(ntest -- > 0)
    sol();
}
/*
3 5
1 2 3 3 1
4 3 2 5 3
2 6 6 2 5
1 2 2 2
2 1 3 3
3 2 3 3
3 5 1 3
*/
