#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
#include <functional>
using namespace std;  
#define int long long
#define double long double
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define M2 1000000009
#define M1 998244353
#define inf 1e18
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> new_data_set;
bool isprime(int n){
    	if(n==1) return false;
    	for(int i = 2;i*i<=n;i++){
    		if(n%i==0) return false;
		}
		return true;
}
int lcm(int a,int b){
	return (a*b)/__gcd(a,b);
}
int gcd(int a, int b){ 
    if (b == 0){
    	return a;
	} 
    return gcd(b, a % b);  
      
} 
int power(int x,int y,int p){  
    int res=1;
    if(x==0)return 0;  
    while(y>0){  
        if(y&1){
        	res*=x;
        	res%=p;
		}   
        y=y>>1;  
        x=(x*x); 
		x%=p;
		res%=p; 
    }  
    return res;  
}
int modInverse(int n, int p){
    return power(n, p-2,p);
}
struct node{
	int a,b;
};






/*    BAAP BAAP HOTA HAI
      Author : Sagar          */
 
 




signed main(){
	FAST
	int n;
	cin>>n;
	int ans=n;
	int tp=1;
	for(int i=1;i<60;i++){
		tp*=2;
		if(tp>n)break;
		int a=n/tp;
		int c=n%tp;
		ans=min(ans,a+i+c);
	}
	cout<<ans;
}

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

int main()
{
  int64_t N;
  cin >> N;
  
  int64_t a, b, c;
  int64_t kai;
  int64_t ans = -1;
  
  if(N == 1) cout << 1 << endl;
  else
  {
    b = 0;
    kai = 1;
    while(true)
    {
      b++;
      kai *= 2;
      if(N < kai) break;
      a = N / kai;
      c = N - a * kai;
      
      if(ans == -1) ans = a + b + c;
      else if(a + b + c < ans) ans = a + b + c;
    }

    cout << ans << endl;
  }
}