#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T,class U> using P = pair<T,U>;
template<class T> using vec = vector<T>;
template<class T> using vvec = vector<vec<T>>;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)


// https://ei1333.github.io/luzhiled/

// combination with fermat theorem
// https://qiita.com/ofutonfuton/items/92b1a6f4a7775f00b6ae

// binary search
// https://qiita.com/drken/items/97e37dd6143e33a64c8c

// modint
// https://qiita.com/DaikiSuyama/items/2dcc49932f2489292e44

int main(){
    ll n; cin >> n;
    ll M = 998244353;
    
    ll a, b, c;
    a = 0;
    b = 0;
    c = 0;
    ll ans = std::numeric_limits<ll>::max();
    ll tmp = 1;
    while(true) {
        if (b == 0) {
            a = n;
            c = 0;
        } else {
            tmp *= 2;
            a = n / tmp;
            c = n - a * tmp;
            if (c < 0) break;
        }
        ans = min(ans, a + b + c);
        b++;
        if (b > 63) break;
        // cout << b << ": " << a + b + c << " = " << a * tmp + c <<  endl;
    }
    cout << ans << endl;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> iint;
typedef pair<ll,ll> llll;
#define ALL(x) (x).begin(),(x).end()
const ll zero = 0;
const ll one = 1;
const ll INF = 9223372036854775807; //10^18
const int inINF = 2147483647; //10^9
const ll MOD = 1000000007; //10^9+7
const ll MOD2 = 998244353;
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int main(){
    ll N;
    cin >> N;
    ll b = 0;
    ll B = 1;
    ll ans = INF;
    while(B <= N){
        ll a = N / B;
        ll c = N - a * B;
        ans = min(ans, a + b + c);
        b++;
        B *= 2;
    }
    cout << ans << endl;

}