




#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define rrep(i,n) for(int i=int(n-1);i>=int(0);--i)
#define fore(i,a) for(auto &i:a)
#define all(x) x.begin(),x.end() 
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define V vector
#define P pair<int,int>
#define TP tuple<int,int,int>
#define F first
#define S second
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
using ll = long long;
using ld = long double;
using namespace std;
const int INF =INT_MAX / 2;
const ll INFL = 1LL << 61;
const int MAX = 2e6+1;

int main() {
    ll n;cin>>n;
    ll x=1;
    ll b=0;
    ll ans=INFL;
    while(x<=n+5){
        ll c=n%x;
        ll a=(n-c)/x;
        chmin(ans,a+b+c);
        x*=2;
        b++;
    }
    cout<<ans<<endl;
    return 0;



}



#include <iostream>
#include <stdio.h>
#include <climits>
#include <math.h>

//martin02

using namespace std;

long long int n;

int main() {
	long long int ans = LLONG_MAX;
	scanf("%lld", &n);

	for (int i = 0; i < 61; i++) {
		long long int b = (long long int) 1 << i;
		long long int a = n / b;
		long long int c = n - a * b;
		ans = min(ans, a + i + c);

	}

	printf("%lld\n", ans);

	return 0;
}