#include <bits/stdc++.h>
#define ll long long
#define vll vector<ll>
#define vpp vector<pair<pair<ll, ll>, ll>>
#define vp vector<pair<ll, ll>>
#define mk make_pair
#define pb push_back
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define N 1000000007
using namespace std;


void fun()
{
    // code begins
    ll n;cin>>n;
    ll a,b,c;
    b = 0;
    ll k = 1;
    ll ans = n;
    while( k <= n)
    {
        a = n/k;
        c = n%k;
        if( a+b+c < ans)
            ans = a+b+c;
        b++;
        k = k*2;
    }
    cout<<ans;
}


int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    IOS;
    ll t; t = 1;
    //cin >> t;
    for (ll i = 1; i <= t; i++)
        fun();
}
#include<algorithm>
#include<bitset>
#include<cmath>
#include<complex>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<iterator>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;
typedef long long ll;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define D()
#define MAXR 100000
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define INITA(a,i,j,v) for(ll k=i;k<=j;k++){a[k]=v;}

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL<<60;
const long long MOD = 1000000007;

int main() {
    ll n; cin >> n;
    ll res = 1e18;
    for (int b=0; b<=60; b++) {
        ll a = n / (1LL << b);
        ll c = n - a * (1LL << b);
        res = min(res, a + b + c);
    }
    cout << res << endl;
    return 0;
}