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

int main(){
  ll n,a,b=0,c=0,p=1,ans=999999999999999999;
  cin >> n;
  while(p<=n){
    a=n/p;
    c=n%p;
    ans=min(ans,a+b+c);
    p*=2;
    b++;
  }
  cout << ans << endl;
}
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll ,ll> pll;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(X) (X).begin(), (X).end()
#define REP(i,x,y) for(ll i = x;i <= y;++i)
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
vector<string> vec_splitter(string s) {
    for(char& c: s) c = c == ','?  ' ': c;
    stringstream ss; ss << s;
    vector<string> res;
    for(string z; ss >> z; res.push_back(z))
        ;
    return res;
}
void debug_out(vector<string> args, int idx) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, Head H, Tail... T) {
    if(idx > 0) cerr << ", ";
    stringstream ss; ss << H;
    cerr << args[idx] << " = " << ss.str();
    debug_out(args, idx + 1, T...);
}
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __VA_ARGS__)
void localTest() {
    #ifndef ONLINE_JUDGE
        freopen("inp", "r", stdin);
        freopen("out", "w", stdout);
    #endif      
}
void solver() {
    ll inf = 2e18, ans = inf, n;
    cin >> n;
    for(ll i = 0; i <= 60; ++i) {
        ll po = (1LL << i);
        if(po > n) {
            break;
        }
        ll c = n % po, a = (n - c) / po;
        ans = min(ans, a + i + c);
    }
    cout << ans << "\n";
    return;
}
int main() {

    localTest();
    fastio;
    int t = 1;
    // cin >> t;
    while(t--) {
        solver();
    }

    return 0;
}