#include<bits/stdc++.h>
using namespace std;
long long pow(int k){
    if (k==0) return 1;
    else return 2*pow(k-1); 
}
int main(){
    long long N;
    cin >> N;
    int k = 0;
    priority_queue<long long , vector<long long> , greater<long long>> pq; 
    while(N>=pow(k)){
        long long a,b;
        a = N/pow(k);
        b = N - a * pow(k);
        long long sum = a + b + k;
        pq.push(sum);
        k++;
    }

    cout << pq.top()  << endl;
}
#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;

int main() {
    uint64_t N;
    cin >> N;

    uint64_t a, b, c;
    uint64_t ans = N;

    for (b = 0; b < 100; b++){
        if (1LL<<b > N) break;
        for (int bb = b; bb < 100; bb++){
            if (1LL<<bb > N) break;
            c = N % (1LL<<bb);
            a = (N - c) / (1LL<<b);
            ans = min(ans, a+b+c);
        }
        // c = N % (1<<b);
        // a = (N - c) / (1<<b);
        // ans = min(ans, a+b+c);
        // cout << b << " " << a  << " " << c  << " " << ans <<endl;
    }
    cout << ans <<endl;
   return 0;
}