#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define sz(x) ((int) x.size())
#define all(x) x.begin(), x.end()
#define debug if (true) cerr
using ll = long long;

ll N;

void solve() {
    cin >> N;
    ll answer = N;
    rep(b, 0, 60) {
        ll B = 1LL << b;
        if (B > N) continue;
        ll A = N / B;
        ll C = N - A * B;
        // cout << A << " " << B << " " << C << endl;
        answer = min(A+b+C, answer);
    }
    cout << answer << '\n';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    solve();
    return 0;
}
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;

LL n;

void into(){
  scanf("%lld",&n);
  LL a=(1LL<<60)-1,b=0,c=0;
  for (LL y=0;1LL<<y<=n;++y){
	LL x=n>>y,z=n&(1LL<<y)-1;
	if (x+y+z<a+b+c) a=x,b=y,c=z;
  }
  //printf("%lld %lld %lld\n",a,b,c);
  printf("%lld\n",a+b+c);
}

void work(){
}

void outo(){
}

int main(){
  int T=1;
  //scanf("%d",&T);
  for (;T--;){
    into();
    work();
    outo();
  }
  return 0;
}