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

#define SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long
#define endl '\n'
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define fill(a,b) memset(a, b, sizeof(a))
#define setbits(x) __builtin_popcountll(x)

const int inf=0x3f3f3f3f3f3f3f3f;
int dx[]={0,1,1,1,0,-1,-1,-1};
int dy[]={1,1,0,-1,-1,-1,0,1};

// <--------------------Code Begins Here-------------------->

int32_t main() {
    SPEED;
    int t = 1;
    // cin>>t;
    while(t--) {
    	int n;
    	cin>>n;
    	int LG = log2(n);
    	// cout<<LG<<endl;
    	int ans = 1e18;
    	for(int i = 0; i <= LG; ++i) {
    		int k = (1ll << i);
    		int c = n % k, a = n / k;
    		ans = min(ans, a + i + c);
    	}
    	cout<<ans;
    }
    return 0;
}
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_cxx;
using ll = long long;
int main() {
  ll n;
  cin >> n;
  ll exp = 1,p = 0,a = 1000000000000000000;
  while(exp <= n){
    a = min(a,p+n/exp+n%exp);
    exp *= 2;
    p++;
  }
  cout << a << endl;
}