#include <bits/stdc++.h>
#include <iostream>
#include <utility>
#include <algorithm>
#include <assert.h>
#include <unordered_map>
//using namespace __gnu_pbds;
using namespace std;
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pb push_back
#define ai(arr,n) for(int i=0;i<n;i++)cin>>arr[i];
#define ao(arr) for(auto tengo_copyright:arr) cout<<tengo_copyright<<" ";
#define mi(arr,m,n) for(int i=0;i<m;i++){ for(int j=0;j<n;j++) cin>>arr[i][j];}
#define mo(arr,m,n) for(int i=0;i<m;i++){ for(int j=0;j<n;j++) cout<<arr[i][j]<<" "; cout<<endl;}
#define ll long long
#define debug cout<<"I AM EXECUTING"<<endl
#define testcases int q_ves; cin>>q_ves; while(q_ves--)
#define vi vector<int>
#define vll vector<long long int>
#define vppo(prs) for(auto x:prs){cout<<x.first<<" "<<x.second<<endl;}
#define For(__,hajmola,adfdf) for(int __ = hajmola; __<adfdf;__++)
#define formax 1e18
#define formin -1e18
#define watch(x) cout << (#x) << " is " << (x) << endl
#define in(x) ll int x; cin>>x
#define stin(x) string x;cin>>x
//#define ordered_multiset tree<pair<ll int,ll int>, null_type, less<pair<ll int,ll int>>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define out1(a) cout<<a<<endl
#define out2(a,b) cout<<a<<"|"<<b<<endl

string sconvert(ll int n)
{
    stringstream ss;
    ss<<n;
    string str = ss.str();
    return str;
}

void  single() {
    in(n);
    ll int sum=1e18,a=1,b=1,c=1,pot=0;
    ll int res=0;
    if (n==0){out1(0);return;}
    while (1) {
    b = pow(2,pot);
    if(b>n)break;
    a = n/b;
    c = n % (a*b);
    res = a+pot+c; 
    if (sum>res){sum=res;}
    pot++;

    } 
    out1(sum);



    return;
}
    
void multiple()
{
    testcases
    {
        single();
    }
}

int main()
{
  IOS;
  //freopen("input.txt","r",stdin);
  //freopen("output.txt","w",stdout);
  single();
}
//g++ -std=c++11 -O2 -Wall a.cpp -o a
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
#pragma GCC optimize("Ofast")
using namespace         std;
#define                 int long long
#define                 double long double
#define                 fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ArrayFill(arr,a)       memset(arr,a,sizeof arr)
#define                 endl '\n'              
#define countBit(x)     __builtin_popcountll(x)
#define all(x)          x.begin() , x.end()
#define ln              cout<<'\n';
using namespace std;
using namespace __gnu_pbds;  
// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> oset; 
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset; 

int ceil(int a,int b){return (a+b-1)/b;}
#define sim template < class c
#define ris return * this
#define dor > debug_ & operator <<
#define eni(x) sim > typename enable_if<sizeof dud<c>(0) x 1, debug_&>::type operator<<(c i) {
sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...);
struct debug_ {~debug_() { cerr << endl; }eni(!=) cerr << boolalpha << i; ris; }eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {ris << "(" << d.first << ", " << d.second << ")";}
sim dor(rge<c> d){*this << "[";for (auto it = d.b; it != d.e; ++it)*this << ", " + 2 * (it == d.b) << *it;ris << "]";}};
#define db(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define debug(x) debug_() << db(x)
int modulo(int a,int b,int c){
    int x=1,y=a%c;
    while(b > 0){
        if(b%2 == 1)
            x=(x*y)%c;
        y = (y*y)%c;
        b = b>>1;
    }
    return  x%c;
}

const int mod = 1e9 + 7ll;
// const int mod = 998244353ll;
const int N = 5001;
const int inf = 1e10;

void solve(){
    int n;
    cin >> n;
    int ans = n;

    for(int i=0;i<=63;i++){
        int x = (1ll<<i);
        int c = i + n/x + n%x;
        ans = min(ans,c);
    }
    cout << ans;
}

signed main(){
    fast;
    int tt{1};
    // cin >> tt;
    for(int ii=1;ii<=tt;ii++){
        // cout << "Case #" << ii << ": ";
        solve();
        ln;
    }
    return 0;
}