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

typedef long long ll;

int main(){
    ll n,ans=LONG_LONG_MAX;
    cin>>n;
    for(int b=0;(1LL<<b)<=n;b++)
        ans=min(ans,b+(n>>b)+(n&((1LL<<b)-1)));
    cout<<ans<<endl;
}
/* HAVE PATIENCE YOU CAN SOLVE EVERY QUESTION
   JUST THINK*/
   //#pragma GCC optimize("Ofast")
   //#pragma GCC optimize ("unroll-loops")
#include<bits/stdc++.h>
//#include <unordered_set>
//#include <unordered_map>

using namespace std;
int mod = 1000000007;
const double pi = 3.141592653689793238460;
const int inf = 0x3f3f3f3f;
const int   N = 2e5 + 5;
const int pr = 31;
#define ll long long 
#define vi vector<int> 
#define pb push_back
#define mp make_pair
#define MEM(x) memset(x,inf,sizeof(x))
#define fi first
#define se second
#define forn(i, n) for (int i = 0; i< n; i++)
#define forl(i,l,u) for(int i=(int)l;i<=(int)u;++i)
#define all(v) (v).begin(), (v).end()
#define sum(a)   ( accumulate ((a).begin(), (a).end(), 0ll))
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;


int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
int f(int x, int y, string a[]) {
    int l;
    if (a[x][y] == '+')l = 1;
    else l = -1;
    return l;
}

int n;
long long dp[501][501];
vector<int > one(n);
vector<int> zero(n);
int main()
{
//#define int ll
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);


    ll n;
    cin >> n;
    ll ans = 1e18;
    forl(i, 0, 61) {
        ll b = (ll)pow(2, (ll)i);
        ll c = n % b;
        ll a = n / b;
        ans = min(ans, (a + (ll)i + c));
    }
    cout << ans << endl;
}


