#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>
//#include <set>
using namespace std;
typedef long long LONG;
 
 
 
LONG gcd(LONG x, LONG y) {
    if(x % y == 0) {
        return y;
    } else {
        return gcd(y, x % y);
        //x%y==0でないときはユーグリットの互除法を使って再帰的に関数を呼び出す。
    }
}
void print(vector<int>A){
    for(size_t i=0;i<A.size();i++){
        if(A[i]==1){
            cout<<'(';
        }else{
            cout<<')';
        }
    }
}
 
bool comp1(pair<int,int> lhs, pair<int,int>rhs) {
    return lhs.first<rhs.first;
}
 
bool comp2(pair<string,int> lhs, pair<string,int>rhs) {
    return lhs.second<rhs.second;
}
void solve(int min,int max,vector<int>&A,int B){
    if(min+1==max){
        cout<<std::min(A[max]-B,B-A[min])<<endl;
        return;
    }
    int a=(min+max)/2;
    if(A[a]<B){
        solve(a,max,A,B);
    }else{
        solve(min,a,A,B);
    }
    
    
    
}
LONG pow2(LONG a,LONG b){
    LONG res=1;
    for(int i=0;i<b;i++){
        res=res*a;
    }
    return a;
}

void solve1(vector<bool>&A,vector<LONG>&C,int pos,vector<vector<pair<int,LONG>>>&path){
    for(int i=0;i<path[pos].size();i++){
        if(!A[path[pos][i].first]){
            A[path[pos][i].first]=true;
            C[path[pos][i].first]=C[pos]^path[pos][i].second;
            solve1(A,C,path[pos][i].first,path);
        }
    }
}

const LONG mod=1000000007;
int main() {
    LONG N;
    cin>>N;
    LONG result=0x1000000000000000;
    LONG pos=1;
    for(int i=0;i<63;i++){
        LONG a=N/pos;
        LONG c=N%pos;
        
        if(a+i+c<result){
            result=a+i+c;
        }
        
        
        pos=pos<<1;
        
    }
    cout<<result<<endl;
    
    
    return 0;
    
 
}

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <list>
#include <ctime>
#include <sstream>
#include <queue>
#include <stack>
#include <random>
#include <bitset>
#include <numeric>
#include <cassert>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
#define rep(x, b, e) for(int x=(b); x<(e); ++x)
#define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x)
#define all(c) c.begin(),c.end()
#define sz(x) ((int)((x).size()))
#define pb push_back
#define st first
#define nd second
#define mp(x,y) make_pair(x,y)
typedef short int sint;

int main() {
	ios_base::sync_with_stdio(false);
	ll n;
	cin >> n;
	ll bRes = n + 1;
	ford(wyk, 61, 0) {
		ll pot = 1LL << wyk;
		if (n >= pot) {
			ll a = n / pot;
			ll c = n - (a * pot);
			bRes = min(bRes, a + wyk + c);
		}
	}
	cout << bRes << endl;
}