
/*
			Sunday 16 May 2021 05:35:27 PM IST
					@uthor::astrainL3gi0N
*/

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

typedef long long int ll;
typedef std::vector<int> vi;
typedef std::pair<int,int> ii;
typedef std::vector<ii> vii;
typedef std::vector<ll> vl;

#define pb push_back
#define mp make_pair
#define debg(x)	std::cerr<<(#x)<<" => "<<x<<'\n';
#define debgg(x,y)	std::cerr<<(#x)<<" => "<<x<<'\t'<<(#y)<<' '<<y<<'\n';
#define len(a) (int)(a).size()
#define all(x) x.begin(),x.end()

const int mod = 1000000007;
const int modd = 998244353;
const int esc = -1;
const string ys = "Yes\n";
const string no = "No\n";

bool comp (int x, int y) {
    return x > y;
}
void printarr (int arr[], int n) {
    for(int i = 0; i < n; ++i)
        cout<<arr[i]<<(i<n-1?' ':'\n');
}
template < typename T> void  printv (T &a) {
    for (auto it = a.begin(); it != a.end(); ++it)
        cout<<*it<<' ';
    cout<<'\n';
}
int gint() {
    int n;  cin>>n;
    return n;
}
//int t[510][510];
ll ans;

void solve () {
    map <int,int> freq;
    set <int> hsh;
    ans = 0;
    bool ok = false;
    ll n;   cin>>n;
    ll a,b,c;
    
    ll num = n;
    ll res = 1;
    int cnt = 0;
    ans = n;

    while (num > 0) {
        cnt++;
        res *= 2;
        a = n/res;
        b = cnt;
        c = n - res*a;
        ans = min(ans,a+b+c);
        
        num /= 2;
    }

    cout<<ans;
    cout<<'\n';
}
void sol () {
    //test
}

int main () {

    //cout << setprecision(15) << fixed;
    int testcases = 1;  
    for (int t = 0; t < testcases; ++t) {
        //cout<<"Case #"<<t+1<<": ";
        solve();
    }

    return 0;
}

#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;
}