#include<bits/stdc++.h>
#define ll              long long int
#define ld              long double
#define ff              first
#define ss              second
#define mod             1000000007
#define ull             unsigned long long
#define vll             vector<ll>
#define uom             unordered_map
#define uos             unordered_set
#define pll             pair<ll, ll>
#define INF             9223372036854775807
#define w(t)            ll tests;    cin>>tests;        while(tests--)
#define endl            "\n"
#define pb(x)           push_back(x)
#define ppb()            pop_back()
#define all(x)          x.begin(), x.end()
#define mp(a, b)        make_pair(a, b)
#define setp(x, y)      fixed << setprecision(x) << y
#define f(i, a, b)      for(ll i = a ; i < b ; i++)
#define fo(j, a, b)     for(ll j = a ; j >= b ; j--)
#define fill(a, n, x)   f(i, 0, n)     a[i]=x;
using namespace std;

void init() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int main() {

    init();
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    ll n;
    cin >> n;

    vll v;
    ll i = 1;
    while (i <= 1e18)
    {
        v.pb(i);
        i *= 2;
    }

    ll mn = INF;
    f(i, 0, v.size())
    mn = min(mn, n / v[i] + i + n % v[i]);

    cout << mn;

    return 0;
}
#include <iostream>
#include <cmath>
#include <climits>
#include <algorithm>

using namespace std;

typedef unsigned long long ull;

int main()
{
    ull N;
    cin >> N;
    ull b = floor(log2(N)), sum = LLONG_MAX;
    for (int i = 0; i <= b; i += 1)
        sum = min(sum, (N / (1ULL << i)) + i + (N % (1ULL << i)));
    cout << sum;
}