#include<bits/stdc++.h>

using namespace std;
using ll = long long;
using pint = pair<int,int>;

// __int128の入出力
 
using LL = __int128;
istream& operator>>(istream& is, LL& v)
{
    string s;
    is >> s;
    v = 0;
    for (int i = 0; i < (int)s.size(); i++) {
        if (isdigit(s[i])) { v = v * 10 + s[i] - '0'; }
    }
    if (s[0] == '-') { v *= -1; }
    return is;
}
 
ostream& operator<<(ostream& os, const LL& v)
{
    if (v == 0) { return (os << "0"); }
    LL num = v;
    if (v < 0) {
        os << '-';
        num = -num;
    }
    string s;
    for (; num > 0; num /= 10) { s.push_back((char)(num % 10) + '0'); }
    reverse(s.begin(), s.end());
    return (os << s);
}
using Int = __int128_t;

int main()
{
    ll N; cin >> N;
    ll ans = 1e18;
    for(int b = 0; b <= 62; b++) {
        ll num = (1LL<<b);
        if(num > N)continue;
        ll a = N / num;
        ll c = N % num;
        ans = min(ans, a + b + c);
    }
    cout << ans << endl;
}

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

int main() {
  long long int A,count=0,B,C,D,E;
  long long int ans[1000]={0};
  cin >> A;
  B=A;
  C=1;
  while(1){
    D=A/C;
    E=A%C;
    ans[count]=count+D+E;
    if(B<2)break;
    B=B/2;
    C=C*2;
    count++;
 }
  long long int min=ans[0]; 
  for(int i=1;i<count;i++){
    if(min>ans[i])min=ans[i];
  }
  cout<<min;
}
