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

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for(int i = (int)(s); i < (int)(n); ++i)

ll INF = 1ll << 60;

int main(){
    ll n;
    cin >> n;
    ll a, b, c;
    b = 0;
    ll ex = 1;
    ll ans = INF;
    while(ex <= n){
        a = n/ex;
        c = n%ex;
        ans = min(ans, a + b + c);
        ex *= 2;
        b++;
    }

    cout << ans << endl;
    
    return 0;
    
}
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <climits>
#include <queue>
#include <map>
using std::cout;
using std::cin;
using std::string;
using std::vector;
using std::endl;

void loadData(vector<long long>& data, int n) {
    for (int i = 0; i < n; i++) {
        cin >> data[i];
    }
}

void loadPowers(vector<long long>& powers) {
    for (int i = 1; i < 63; i++) {
        powers[i] = 2 * powers[i - 1];
    }
}

int main()
{
    long long n;
    cin >> n;
    long long ans = LLONG_MAX;
    vector<long long> powers(63, 1);
    loadPowers(powers);

    for (long long i = 0; powers[i] <= n; i++) {
        long long j = n / powers[i];
        long long k = n - j * powers[i];
        ans = std::min(ans, i + j + k);
    }

    cout << ans << endl;
    return 0;
}