#include <bits/stdc++.h>
using namespace std;

#define sz(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 1000000007
#define endl "\n"
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pi;
ll zero = 0;
ll one = 1;

int fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}

int main()
{
    fast();
    cout << setprecision(12);
    ll ttt = 1;
    // cin >> ttt;
    while (ttt--)
    {
        // Code Here
        // cout<<"nice"<<endl;
        ll n;
        cin >> n;
        ll answer = 1e18;
        for (int i = 0; i < 65; i++)
        {
            ll a, b = (ll)pow(2, i), c;
            a = n / b;
            c = n % b;
            // cout << a << " " << b << " " << c << endl;
            answer = min(answer, a + i + c);
            if (a == 0)
                break;
        }
        cout << answer << endl;
    }
    return 0;
}

#include<bits/stdc++.h>
#include<tuple>
typedef long long ll;
#define INF 9999999999
#define mod 1000000007
#define eps 1e-9
#define pi acos(-1)
using namespace std;
typedef pair<ll,ll>du;
ll n,ans,res;
int main(){
    ios::sync_with_stdio(0);
    cin>>n;
    ans=INF;
    for(ll i=0;(n>>i)!=0;i++){
        if((n>>i)&1){
            res=i+(n>>i)+n%((ll)1<<i);
            //cout<<(1<<i)<<endl;
            ans=min(ans,res);
        }
    }
    cout<<ans<<endl;
    return 0;
}