#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define rep(i, n) for(int i = 0; i < n; i++)

int main()
{
    ll n;
    cin >> n;
    ll m = n;
    ll b2 = 1;
    ll ans = 1e18+1;
    for(int b = 0;b2<=n;b++){
        ll a = n/b2;
        ll c = n%b2;
        ans = min(ans,a+b+c);
        b2 *= 2;
    }
    cout << ans << endl;
    return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    long long n;
    cin >> n;
    long long sum=n;
    long long a=n,b=0,c=0;
    long long count2=1;
    while(a>1) {
        if(a%2==0) {
            b++;
            a/=2;
            count2*=2;
        }
        else {
            c=c+count2;
            a-=1;
        }

        if(sum>=a+b+c) {
            sum=a+b+c;
        }
    }

    cout << sum << endl;
}