#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#define ll long long
#define MAXN 300005
#define uns unsigned
#define INF 0x3f3f3f3f
using namespace std;
inline ll read(){
	ll x=0;bool f=1;char s=getchar();
	while((s<'0'||s>'9')&&s>0){if(s=='-')f^=1;s=getchar();}
	while(s>='0'&&s<='9')x=(x<<1)+(x<<3)+s-'0',s=getchar();
	return f?x:-x;
}
ll n,s;
int main()
{
	n=read(),s=n;
	for(int b=0;(1ll<<b)<=n;b++){
		ll B=(1ll<<b),a=n/B,c=n-a*B;
		s=min(s,a+b+c);
	}
	printf("%lld\n",s);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long diff;
map<long long, long long> dp;

long long fun(long long n, long long m)
{
    if(n>=m)
        return dp[m] = (n-m);
    long long res=m-n;
    if(m%2)
    {
        long long r1, r2;
        if(!dp[m-1])
            r1 = fun(n, m-1);
        else
            r1 = dp[m-1];
        if(!dp[m+1])
            r2 = fun(n, m+1);
        else
            r2 = dp[m+1];
        res = min(res, 1+min(r1, r2));
    }
    else
    {
        long long r3;
        if(!dp[m/2])
            r3 = fun(n, m/2);
        else
            r3 = dp[m/2];
        res = min(res, 1+r3);
    }
    return dp[m] = res;
}

int main()
{
    long long n, m;
    cin >> n >> m;
    long long res = fun(n, m);
    cout << res;
    return 0;
}
