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

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
const int N = 1e5 + 10, M = 3e5 + 10;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
#define x first
#define y second
const int mod = 1e9 + 7;
#define gcd __gcd

void solve() {
	LL n;
	scanf("%lld", &n);
	LL res = 2e18;
	for(LL i = 0; (1LL << i) <= n; i++) {
		LL a = n / (1LL << i);
		LL c = n % (1LL << i);
		res = min(res, a + i + c);
	}
	printf("%lld\n", res);
}

int main() {
	// freopen("in.txt", "r", stdin);
    // int t; cin >> t; while(t--)
    solve();
    return 0;
}

#include <bits/stdc++.h>
#include <string>
 
typedef long long ll;
typedef long double ldb;
typedef double db;
using namespace std;
 
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ins insert
#define mp make_pair
#define len(s) s.length()
#define forp(i, a, b) for (ll i = a; i <= b; i++)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define ren(i, n) for (ll i = n - 1; i >= 0; i--)
#define forn(i, a, b) for (ll i = a; i >= b; i--)
#define on cout << endl
#define o2(a, b) cout << a << " " << b
#define os cout << " "
#define all(v) v.begin(), v.end()
#define mem(n, m) memset(n, m, sizeof(n))
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vld vector<ld>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define vvld vector<vector<ld>>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define mll map<ll, ll>
#define lb lower_bound
#define ub upper_bound
#define ret return 0
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define ford(container, it) for (__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define fors(container, it, a, b) for (__typeof(container.begin()) it = a; it != b; it++)
#define d1(x) cout << (x) << endl
#define d2(x, y) cout << (x) << " " << (y) << endl
#define d3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define d4(a, b, c, d) cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define itoa(x) to_string(x);
#define atoi(x) stoll(x);
 
#define boost ios_base::sync_with_stdio(0)
#define MOD 1000000007
#define EPSILON 1e-9
#define PI 3.14159265358979323846
#define inf 999999999999999999
#define siz 100001
#define SIZ 1000001
#define SIZE 200001
 
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define w(x)  \
    int x;    \
    cin >> x; \
    while (x--)
 
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
ll pwr(ll x, ll y)
{
    ll res = 1;
    x = x;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x);
        y = y >> 1;
        x = (x * x);
    }
    return res;
}
ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (b * a) / gcd(a, b);
}
ll fact(ll n)
{
    if (n <= 1)
        return 1;
    return n * fact(n - 1);
}
ll nPr(ll n, ll r)
{
    return fact(n) / fact(n - r);
} 
void c_p_c()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
ll f(ll n)
{
    ll m = 0;
    while(n>0)
    {
        m = max(m, n%10);
        n/=10;
    }
    return m;
}
// int dp[1005][100005]; //#books vs pricelimit
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    c_p_c();
    ll tt;
    tt = 1;
    // cin >> tt;
    while (tt-- > 0)
    {      
        ll n;
        cin>>n;
        ll b = 0, a = n, c = 0, q = a*pwr(2, b);
        ll m = a+b+c;
        while(pwr(2, b)<=n)
        {
            b++;
            a = n/pwr(2, b);
            c = n%pwr(2, b);
            m = min(a+b+c, m);
        }
        d1(m);
    }

    return 0;
}