#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);


    ll a,b,c,ans,n;
    cin >> n;

    b = 0LL;
    ll two = 1LL;
    ans = 1e18;
    while(two <= n)
    {
        c = n % two;
        a = n / two;
        ans = min(ans, a+b+c);
        b++;
        two *= 2LL;

    }
    cout << ans << endl;

}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
using namespace std;
using ll = long long;
const int INF = 1e9;
const ll LINF = 1e18;
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        os << v[i];
        if (i < sz(v) - 1) os << ' ';
    }
    return os;
}
int main() {
    ll n;
    cin >> n;
    ll ans = LINF;
    for (ll b = 0; b < 60; b++) {
        ll x = 1ll << b;
        for (ll a = max(0ll, n / x - 100); n - a * x >= 0 && a <= n / x + 100;
             a++) {
            ll c = n - a * x;
            chmin(ans, a + b + c);
        }
    }
    cout << ans << '\n';
}