// Cookie197
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<iomanip>
#include<math.h>
using namespace std;
#define no_tle ios::sync_with_stdio(false); cin.tie(0)
#define ll long long
#define pii pair<ll,ll>
#define mp make_pair
#define si short
#define mod 1000000007
#define endl '\n'
int main(){
    ll n,ans;
    cin>>n;
    ans=n;
    ll b;
    for(b=0;b<66;b++){
        if(pow(2,b)>n) break;
        ll tmp=pow(2,b);
        ans=min(ans,n-(n/tmp)*tmp+b+(n/tmp));
    }
    cout<<ans<<endl;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <vector>
#include <iomanip>
#include <math.h>
using namespace std;
typedef long long ll;
ll pow(ll a)
{
    ll ans = 1;
    while (a--)
    {
        ans *= 2;
    }
    return ans;
}
int main() 
{
    ll n; cin >> n;
    ll a, b, c;
    ll l = 60;
    while (pow(l) > n)
    {
        l --;
    }
    ll ans = 1e18 + 5;
    for (ll i = l; i >= 0; i--)
    {
        b = i;
        a = n / pow(b);
        c = n - a * pow(b);
        ans = min(ans, a + b + c);
        
    }
    //cout << a << ' ' << b << ' ' << c << endl;
    cout << ans << endl;


  
    
    return 0;
}
