Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially, the display shows number n.Bob wants to get number m on the display. What minimum number of clicks he has to make in order to achieve this result?

The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 104), separated by a space .

Print a single number — the minimum number of times one needs to push the button required to get the number m out of number n.

NoteIn the first example you need to push the blue button once, and then push the red button once.In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.

#include <iostream>
#include <vector>
#include <queue>

using namespace std;
int n;
int m;
int s;
vector<int> adj[20001];
vector <int> result;
int used[20001];

void bfs(int n ,int m) {
	queue<int> q;
	q.push(n);
	used[n] = 0;
	while (!q.empty()) {
		int v = q.front();
		q.pop();
		if(v==m){
            cout<<used[v]<<endl;
            return;
		}
		int a = v*2;
		if(a<=20000 && used[a]==-1)
        {
            used[a] = used[v]  + 1;
            q.push(a);
        }
        int b = v  -1 ;
        if(b>0 && used[b]==-1)
        {
            used[b] = used [v] +1;
            q.push(b);
        }
	}
}

void readData() {
    cout<<"nim of node then num of edge\n";
	cin >> n >> m;
    int v, w;
    cout<<"inset\n";
	for (int i = 0; i < m; ++i) {

		cin >> v >> w;
		adj[v].push_back(w);
		adj[w].push_back(v);
	}
}
void fillArray()
{
    for(int i=0;i<20001;i++)
        used[i]=-1;
}
int main()
{
    int n , m;
    cin>>n>>m;
    fillArray();
	bfs(n,m);

	return 0;
}

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

using ll=long long;
using VL=vector<ll>;
using VC=vector<char>;

/**
 * 520B.
 * @brief main
 * @return
 */
int main()
{
    ll n,m;
    cin >> n >> m;

    ll limit = max(n,m);
    limit *= 2;

    VL visited(limit);
    VL dist(limit);

    queue<ll> q;
    q.push(n);
    dist[n] = 0;

    while(!q.empty())
    {
        ll curr = q.front();
        q.pop();
        if(curr == m)
        {
            cout << dist[curr] << endl;
            return 0;
        }
        if(visited[curr] == 1)
        {
            continue;
        }
        visited[curr] = 1;
        if(curr > 0 && visited[curr-1] == 0)
        {
            dist[curr-1] = dist[curr]+1;
            q.push(curr-1);
        }
        if(curr*2 < limit && visited[curr*2] == 0)
        {
            dist[curr*2] = dist[curr]+1;
            q.push(curr*2);
        }
    }
    cout << -1 << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        int cnt=0;
        if(n>=m)
            cout<<n-m;
        else{
            while(n!=m)
            {
                if(m&1)
                {

                    m+=1;
                    cnt++;
                }
                m/=2;
                cnt++;
                if(n>m)
                {

                    cnt+=n-m;
                    break;
                }
            }
            cout<<cnt;
        }
    }
    return 0;
}

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
struct sl
{
	int dis,id;
} t[1000000];
int f[20000],i,j,n,m,k,l,o,p,head,tail;
int main()
{
	scanf("%d%d",&n,&m);
	memset(f,0x777,sizeof (f));
	f[n]=0;
	t[++tail].id=n;
	while (head<tail)
	{
		int x=t[++head].id;
		if (x<m&&t[head].dis+1<f[x*2]) t[++tail].id=x*2,t[tail].dis=t[head].dis+1,f[x*2]=t[tail].dis;
		if (x>0&&t[head].dis+1<f[x-1]) t[++tail].id=x-1,t[tail].dis=t[head].dis+1,f[x-1]=t[tail].dis;
	}
	printf("%d",f[m]);
}

#include <iostream>
#include <vector>
#include <climits>
#include <unordered_map>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
int getMin(vector<int>* edges,int n,int m,int p){
	bool*visited=new bool[2*p+1]();
	unordered_map<int,int> mi;
	queue<int> pendingNodes;
	pendingNodes.push(n);
	visited[n]=true;

	while(!pendingNodes.empty()){
		int curr=pendingNodes.front();
		pendingNodes.pop();
		for(int i=0;i<edges[curr].size();i++){
			if(!visited[edges[curr][i]]){
				visited[edges[curr][i]]=true;
				pendingNodes.push(edges[curr][i]);
				mi[edges[curr][i]]=curr;
				if(edges[curr][i]==m){
					break;
				}
			}
		}
	}
	int ans=0;
	int curr=m;
	while(curr!=n){
		curr = mi[curr];
		ans++;
	}
	return ans;
}
int main(){
	int n,m;
	cin >> n >> m;
	int p = n>m ? n: m;
	vector<int>* edges=new vector<int>[2*p+1];

	for(int i=(2*p);i>=1;i--){
		edges[i].push_back(i-1);
	}
	// for (int i = 0; i < edges[10].size();i++)
	// {
	// 	cout<<edges[10][i]<<" ";
	// }
	for(int i=1;i<=p;i++){
		edges[i].push_back(2*i);
	}

	cout<<getMin(edges,n,m,p);
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;

vector<int>v[10004];
bool vis[10004];
bool pushed[10004];
int dist[10004];

void DFS(int node,int par)
{
	vis[node] = 1;
	dist[node] = dist[par] + 1;
	//cout << node << " -> ";
	for (auto child : v[node])
	{
		if (!vis[child])
			DFS(child,node);

	}
}

int main()
{
	int n, m;
	bool stop = false;
	bool stop1 = false;
	cin >> n >> m;
	queue<int>q;
	q.push(n);
	while (!q.empty())
	{
		int front = q.front();
		if (front > 0 && !pushed[front - 1])
		{
			if (n<m&&front - 1 > m+1)stop1 = true;
			else stop1 = false;
			if (!stop1)
			{
				q.push(front - 1);
				v[front].push_back(front - 1);
				pushed[front - 1] = 1;
			}
		}
		if (n<m&&!pushed[front * 2])
		{
			if (front * 2 > m+1)stop = true;
			else stop = false;
			if (!stop)
			{
				q.push(front * 2);
				v[front].push_back(front * 2);
				pushed[front * 2] = 1;
			}
		}
		if (front - 1 == m || front * 2 == m)
			break;
		q.pop();
	}
	DFS(n, 0);
	cout <<dist[m]-1;

}

#include<stdio.h>
#include<string.h>
#include<cmath>
#include<cstring>
#include<stack>
#include<iostream>
#include<string>
#include<queue>
using namespace std;
int c[10000000];
struct node
{
	int a,b;
};
queue<node> x;
int main()
{
	int a,b;
	scanf("%d %d",&a,&b);
	memset(c,0,sizeof(c));
	node y;
	y.a=a;
	y.b=0;
	x.push(y);
	c[a]=1;
	int ans;
	while(1)
	{
		node temp=x.front();
		x.pop();
		if(temp.a==b)
		{
			ans=temp.b;
			break;
		}
		else
		{
			temp.b++;
			if(temp.a<0)
				continue;
			if(b>=temp.a)
			{
				temp.a--;
				if(c[temp.a]==0)
				{
					x.push(temp);
					c[temp.a]=1;
				}
				temp.a++;
				temp.a*=2;
				if(c[temp.a]==0)
				{
					x.push(temp);
					c[temp.a]==1;
				}
			}
			else if(temp.a>b)
			{
				temp.a--;
				if(c[temp.a]==0)
				{
					x.push(temp);
					c[temp.a]==1;
				}
				temp.a++;
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}

#include <stdio.h>

int main()
{
    int n,m,t=0,ans;
    scanf("%d %d",&n,&m);
    if(n>=m)
        printf("%d\n",n-m);
    else
    {
        while(m>n)
        {
            if(m%2==1)
            {
                m++;
                t++;
            }
            m/=2;
            t++;
        }
        ans=t+n-m;
        printf("%d\n",ans);
    }
    return 0;
}

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
using namespace std;
int q[100005],step[100005],vis[100005];
void bfs(int x,int end)
{
	int head=0,tail=1;
	q[tail]=x;
	step[x]=0;
	vis[x]=1;
	while(head<tail)
	{
		++head;
		int cur=q[head];
		if(cur==end)
		{
			cout<<step[cur]<<endl;
			return;
		}
		int tmp_1=cur-1;
		if(0<=tmp_1&&tmp_1<=2*end)
		{
			if(!vis[tmp_1])
			{
				vis[tmp_1]=1;
				q[++tail]=tmp_1;
				step[tmp_1]=step[cur]+1;
			}
		}
		int tmp_2=cur*2;
		if(0<=tmp_2&&tmp_2<=2*end)
		{
			if(!vis[tmp_2]){
				vis[tmp_2]=1;
				q[++tail]=tmp_2;
				step[tmp_2]=step[cur]+1;
			}
		}
	}
}
int main()
{
	int s,t;
	while(cin>>s>>t)
	{
		if(s>=t)
		{
			cout<<s-t<<endl;
		}
		else
		{
			bfs(s,t);
		}
	}
	return 0;
}

#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
	int n,m,count=0;
	scanf("%d %d",&n,&m);
	while(n<m){
		if(m%2==0)
			m/=2;
		else
			m++;
		 count++;
    }
    printf("%d\n",count+n-m);
    return 0;
}

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

const int N = 50005;
const int eps = 100000000;
vector<int>adj[N];
int dis[N];
int vis[N];
int n,k;

#define valid(a) a > 0 and a <= N
#define MAX N

int bfs(int n , int m)
{
    //queue<int>q;
    for(int i = 0; i < N;i++)dis[i] = 12250;
    queue<int>q;
    q.push(n);
    dis[n] = 0;
    while(!q.empty()){
        int u = q.front();
        q.pop();

        int v = 2 * u;

        if(v < MAX && dis[v] > dis[u] + 1){
            dis[v] = dis[u] + 1;
            q.push(v);
        }

        v = u - 1;

        if(v >= 0 && dis[v] > dis[u] + 1){
            dis[v] = dis[u] + 1;
            q.push(v);
        }
    }
    return dis[m];
}

int main()
{

    cin >> n >> k;

    memset(vis,0,sizeof vis);
    cout << bfs(n,k) << endl;

    return 0;
}

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n;
bitset <1000000> bs;
ll lvl[100000] ;
int main()
{
    ll n , m , a = 0 ;
    cin >> n >> m ;
    queue < ll > q ;
    memset( lvl , 0 , sizeof lvl );
    q.push(n);
    bs[n] = 1;
    lvl[n] = 0;
    while ( !q.empty() )
    {
        ll w = q.front();
        q.pop();
        bs[w] = 1;
        if ( w == m )
            return cout << lvl[w] , 0;

        if ( !bs[w-1] && w > 1 )
        {
            q.push(w-1);
            lvl[w-1] = lvl[w] + 1 ;
        }
        if ( !bs[2*w] && 2 * w <= m + 3 )
        {
            q.push(2*w);
            lvl[2*w] = lvl[w] + 1;
        }
    }
}

#include <iostream>
#include <queue>
#include <cmath>
using namespace std;

int polje[100000];

void bfs(int n, int m){
    queue<pair<int,int>> Q;
    pair<int,int> a(n,0);
    Q.push(a);
    while(!Q.empty()){
        pair<int,int> temp = Q.front();
        Q.pop();

        if(temp.first == m){
            return;
        }
        else if(temp.first >= 0){
            pair<int,int> a(temp.first * 2,temp.second + 1);
            pair<int,int> b(temp.first - 1,temp.second + 1);
            if(polje[a.first] >= a.second){
                Q.push(a);
                polje[a.first] = a.second;
            }
            if(b.first >= 0 && polje[b.first] >= b.second) {
                Q.push(b);
                polje[b.first] = b.second;
            }

        }
    }
}

int main() {
    int n, m; cin >> n >> m;
    for(int i = 0;i < 2* max(m,n) + 5;i++) polje[i] = INT_MAX;
    bfs(n,m);
    cout << polje[m] << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define ll long long

int n, m;
bool visited[20005];

int bfs() {
	queue< pair<int,int> > q;
	q.push( make_pair(n, 0) );
	while(!q.empty()) {
		pair<int,int> current = q.front();
		q.pop();
		int current_node = current.first;
		if (visited[current_node])
			continue;
		visited[current_node] = true;
		int current_cost = current.second;
		if (current_node == m)
			return current_cost;
		int next_cost = current_cost+1;
		if (current_node * 2 <= 20000)
			q.push(make_pair(current_node * 2, next_cost));
		if (current_node - 1 > 0)
			q.push(make_pair(current_node - 1, next_cost));
	}
}

int main() {

	cin >> n >> m;
	cout << bfs() << endl;

	return 0;
}

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>

using namespace std;
typedef long long ll;
//这是模拟解法，对一个数要么奇或偶，所以就两种情况，二倍除下去，或者减
int main() {
	int n, m;
	cin >> n >> m;
	int ans = 0;
	if (n > m) {
		ans = n - m;
	}
	else {
		while (n < m) {
			if (m % 2) {
				m++;
				ans++;
			}
			m /= 2;
			ans++;
		}
		ans += n - m;
	}
	cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fo(i,x,n) for(ll i=x;i<n;i++)
#define MAX(a,b,c) = max(a,max(b,c))

using namespace std;
ll MOD = 1e9+7;

ll gcd(ll a, ll b){
    if(a == 0){
        return b;
    }
    return gcd(b % a, a);
}

ll powx(ll n, ll p){
    ll res = 1;
    while(p){
        if(p & 1){
            res = (res * n) % MOD;
            --p;
        }
        else{
            n = (n * n) % MOD;
            p /= 2;
        }
    }
    return res;
}
ll cmp(ll a,ll b)
{
    return a>= b;
}
void tc()
{
    ll n,m;
    cin >> n >> m;
    if(n>=m)
    {
        cout << abs(n-m) << endl;
        return;
    }
    ll count = 0;
    while(n!=m)
    {
        if(m%2==0&&n<m)
        {
            m = m/2;
        }
        else
        {
            m++;
        }
        count++;
    }

    cout << count << endl;
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll t;
    t = 1;
   //cin >> t;
    while(t--)
    {
       tc();
    }

}

#include<bits/stdc++.h>

#define FOR(i,a,b) for(auto i=a; i!=b+1-2*(a>b); i+=1-2*(a>b))
#define REP(i,a,b) for(auto i=a-(a>b); i!=b-(a>b); i+=1-2*(a>b))
#define ALL(v) v.begin(),v.end()
#define what_is(x) cout<<#x<<" is "<<x<<endl;
#define min3(a,b,c) min(min(a,b),c)
#define SIZE 1000010
#define MAXN 242
#define PI 3.141592653589793
#define open_read1 freopen("C:\\Users\\Hepic\\Desktop\\a.txt","r",stdin)
#define open_read freopen("ariprog.in","r",stdin)
#define open_write freopen("ariprog.out","w",stdout)

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

struct info
{
    LL vertex,counts;
};

LL N,M;
LL node,cnt,answer=-1;
queue<info> Que;
map<LL,bool> visited;

void bfs(LL starting_num,LL starting_cnt)
{
    Que.push({starting_num,starting_cnt});
    visited[starting_num]=true;

    while(!Que.empty())
    {
        node=Que.front().vertex;
        cnt=Que.front().counts;
        Que.pop();

        if(node>=M)
        {
            answer = (answer==-1) ? cnt+node-M : min(answer, cnt+node-M);
            continue;
        }

        if(node<0)
            continue;

        if(!visited.count(node-1))
        {
            Que.push({node-1,cnt+1});
            visited[node-1]=true;
        }

        if(!visited.count(2*node))
        {
            Que.push({2*node,cnt+1});
            visited[2*node]=true;
        }
    }

}

int main()
{
    scanf("%I64d%I64d",&N,&M);

    bfs(N,0);
    printf("%I64d\n",answer);

    return 0;
}

#include<bits/stdc++.h>
#define ULL unsigned long long
#define N 1000010
using namespace std;
ULL pow_2[64];
int ans;
int get_ans(ULL a)
{
	int cnt=0,i=ans;
	while(a>0&&i>=0)
	{
		while(a>=pow_2[i])
		{
			a-=pow_2[i];
			cnt++;
		}
		i--;
	}
	return cnt;
}
int main()
{
	pow_2[0]=1;
	for(int i=1;i<=63;i++) pow_2[i]=pow_2[i-1]<<1;
	int op,ed;//滑稽
	cin >> op >> ed;
	while(op<ed)
	{
		op<<=1;
		ans++;
	}
	cout << (ans+get_ans((ULL)(op-ed)));
}

    #include <iostream>
    #include <cctype>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <stack>
    #include <queue>
    #include <vector>

    using namespace std;
    const int Max = 2e4+4;
    const int Min = 0;

    typedef struct {  //BFS
        int num;
        int step;
    }node;

    bool vis[Max+5];

    void bfs(int n, int m){
        //const int nn = n-n/2;
        //const int mm = m + m/2;
        queue <node> q;
        memset(vis, false, sizeof(vis));
        vis[n] = 1;
        node st;
        st.num = n; st.step = 0;
        q.push(st);
        while(!q.empty()){
            node temp = q.front();
            q.pop();
            node cur1, cur2;
            cur1 = cur2 = temp;
            cur1.num *= 2; cur1.step ++;
            cur2.num -= 1; cur2.step ++;
            if(cur1.num == m){
                cout << cur1.step; break;
            }
            if(cur2.num == m){
                cout << cur2.step; break;
            }
            //if(cur1.num > nn&& cur1.num < mm)
            //if(cur1.num > Min && cur.num < M)
            if(cur1.num > 0&&cur1.num < Max&&!vis[cur1.num]){
                q.push(cur1);
                vis[cur1.num] = 1;
            }
            //if(cur2.num > nn && cur2.num < mm)
            if(cur2.num > 0&&cur2.num < Max&&!vis[cur2.num]){  //终止点
                q.push(cur2);
                vis[cur2.num] = 1;
            }
         }
    }

    int main(){
        int n, m, c = 0;
        cin >> n >>m;
        if(m < n){
            cout << n-m <<endl;
            return 0;
        }
        else if(m == n){
            cout << 0<<endl;
            return 0;
        }
        else{
            bfs(n, m);
        }
        return 0;
    }

#include <bits/stdc++.h>
using namespace std;
struct node
{
    int val,cnt;
}cur,temp;
bool operator < (node a,node b)
{
    if(a.cnt!=b.cnt)return a.cnt<b.cnt;
    return a.val<b.val;
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,m;
    cin>>n>>m;
    set<node>s;
    cur.cnt=0;
    cur.val=n;
    s.insert(cur);
    while(true)
    {
        cur=*s.begin();
        s.erase(s.begin());
        if(cur.val==m)break;
        else if(cur.val>m)
        {
            cur.cnt+=(cur.val-m);
            cur.val=m;
        }
        else
        {
            if(cur.val!=1)
            {
                temp.val=cur.val-1;
                temp.cnt=cur.cnt+1;
                s.insert(temp);
            }
            cur.val*=2;
            cur.cnt+=1;
        }
        s.insert(cur);
    }
    cout<<cur.cnt<<endl;
    return 0;
}

#include <bits/stdc++.h>
#define  F first
#define  S second
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define all(v) v.begin(),v.end()
#define LightedDigits(x) __builtin_popcount(x)
#define mp make_pair
#define mt make_tuple
#define pb push_back
using namespace std;
int n , m ;
bool vis[(int)2e4+1] ;
int bfs(){
    queue< pair<int , int> > q ;
    q.push({n , 0}) ;
    vis[n] = 1 ;
    while(!q.empty()){
        pair<int , int> state = q.front() ;q.pop() ;
        if (state.first == m)return state.second ;
        if ( !vis[state.first - 1]  && (state.first - 1) > 0 ){
            q.push( {state.first - 1 , state.second + 1} ) ;
            vis[state.first - 1] = 1 ;
        }
        if ( !vis[state.first << 1] && state.first << 1 <= (int)2e4 ){
            q.push( {state.first << 1 , state.second + 1} ) ;
            vis[state.first << 1] = 1 ;
        }
    }
}
int main()
{
    cin >> n >> m ;
    cout << bfs() ;
}

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstdlib>
#include <map>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 1e4 + 10;
struct Node {
    int num , dist;
    Node(int _num, int _dist) {
        this->num = _num;
        this->dist = _dist;
    }
};
map<int,bool> vis;
int n, m, ans;
int bfs(int star) {
    if(n > m) return n - m;

    queue<Node> que;
    vis.clear();

    que.push(Node(star, 0));
    vis[star] = true;
    int dist, cur;
    while(!que.empty()) {
        Node front = que.front();
        que.pop();

        cur = front.num;
        dist = front.dist;
        if(cur == m) return front.dist;
        if(cur <= 0) continue;
        if(cur <= 2*m && !vis[cur-1]) {
            que.push(Node(cur-1, dist + 1));
            vis[cur-1] = true;
        }
        if(!vis[cur*2]) {
            que.push(Node(cur*2, dist + 1));
            vis[cur*2] = true;
        }
    }
    return INF;
}
int main() {
    while(scanf("%d%d", &n , &m) != EOF) {
        ans = bfs(n);
        printf("%d\n", ans);
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#ifndef ONLINE_JUDGE
#define cin in
#endif

const int MAX = 10000;
bool used[MAX + 1];

int main() {
    ios_base::sync_with_stdio(false);
    ifstream in("/home/lionell/Developing/acm/codeforces/round_295/in.txt");
    int n = 0, m = 0;
    cin >> n >> m;
    queue<pair<int, int>> q;
    q.push({n, 0});
    while (q.front().first != m && !q.empty()) {
        pair<int, int> current = q.front();
        if (current.first > 1 && used[current.first - 1] == false)
        {
            q.push({current.first - 1, current.second + 1});
            used[current.first - 1] = true;
        }
        if (current.first * 2 <= MAX && used[current.first * 2] == false) {
            q.push({current.first * 2, current.second + 1});
            used[current.first * 2] = true;
        }
        q.pop();
    }
    cout << q.front().second;
    return 0;
}

#include <cstdio>
#include <cstring>
#define mins(a,b) a<b?a:b
#define MAXN 10010
int m, n, dp[MAXN];
bool vis[MAXN] = {false};
int dfs(int x)
{
    if (x >= 10010 || x <= 0)
        return MAXN;
    if (dp[x] != -1)
        return dp[x];
    if (vis[x])
        return MAXN;
    vis[x] = true;
    if (x & 1)
        dp[x] = dfs(x + 1) + 1;
    else
        dp[x] = mins(dfs(x >> 1) + 1, dfs(x + 1) + 1);
    return dp[x];
}
int main(int argc, char* argv[])
{
    memset(dp, -1, sizeof(dp));
    scanf("%d%d", &n, &m);
    dp[n] = 0;
    printf("%d\n", dfs(m));
    return 0;
}

#include <iostream>
using namespace std;

int main()
{
    int n, m, w(0);
    cin >> n >> m;
    while (n != m)
    {
        if (m > n)
            if (m % 2 == 0) m /= 2, w++;
            else m++, w++;
        else if (n > m) m++, w++;
    }
    cout << w << endl;
    return 0;
}

#include <iostream>
#include <cmath>
using namespace std;

int main() {
	long long n,m;
	cin>>n>>m;
	long long x=0;
	int l=m;
	while(m>n)
	{
		if(m%2!=0)
			m++;
		else m/=2;
		x++;
		if(x>10000)
		{
			x=l;
			break;
		}
	}
	if(m<n)
		x+=n-m;
	cout<<x;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
// typedef long long long long;
int dp[20020];

int main() {
    int N, M;
    int count = 0;
    cin >> N >> M;
    int m = M;

    if(N >= M) {
        cout << N - M << endl;
        return 0;
    }

    memset(dp, -1, sizeof(dp));
    dp[m] = 0;

    while(m > 1) {
        if(m % 2 == 0) {
            m /= 2;
        } else {
            m += 1;
        }
        count++;
        dp[m] = count;
    }

    int ans = 1 << 29;
    for(int step = 0; N < 2 * M ; N *= 2, step++) {
        int i = 0;
        while(true) {
            if(dp[N-i] != -1) {
                break;
            }
            i++;
        }
        ans = min(ans, step + dp[N-i] + i);
    }

    cout << ans << endl;
}

//In the name of God
//-----gmmj

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <map>
#include <sstream>
#include <ctime>

#define Time printf("\nTime : %.3lf s.\n", clock()*1.0/CLOCKS_PER_SEC);
#define Rep(I,N) for(int I=0;I<N;++I)
#define For(J,R,K) for(int J=R;J<K;++J)
#define MP make_pair
#define ALL(X) (X).begin(),(X).end()
#define SF scanf
#define PF printf
#define pii pair<int,int>
#define Ff first
#define Ss second
#define Sort(v) sort(ALL(v))
#define Test freopen("a.in","r",stdin);
#define pb push_back
#define RLL(X) scanf("%I64d",&X);
#define RRLL(X,Y) scanf("%I64d %I64d",&X,&Y);
#define WLL(X) printf("%I64d",X);
#define WWLL(X,Y) printf("%I64d %I64d",X,Y);
#define Loop(N) RLL(N)Rep(i,n)

typedef long long ll;

using namespace std;

queue<pii> q;
bool flag[100009];
ll goal;

ll bfs(ll root){
	int cnt=0;
	/*if(root*30<=goal){
		root*=8,cnt+=3;
	}*/
	q.push(MP(root,cnt));
	pii now;
	while(q.front().Ff!=goal){
		now=q.front();
		q.pop();
		if(!flag[now.Ff]){
			if(now.Ff>0&&now.Ff<goal+20){
				q.push(MP(now.Ff*2,now.Ss+1));
				q.push(MP(now.Ff-1,now.Ss+1));
			}
		}
		flag[now.Ff]=1;
	}
	return q.front().Ss;
}

int main(){
	ll n;
	cin>>n>>goal;
	if(n>goal){
		cout<<n-goal;
		return 0;
	}
	cout<<bfs(n);
	return 0;
}

#include <iostream>
#include <queue>
#include <cmath>
#include <memory.h>

using namespace std;

int n, m;
const int N = 1e5;
bool vis[N];
queue<int> q;

int BFS() {
    memset(vis, false, sizeof vis);

    q.push(n);

    int cost = 0, size;

    while(!q.empty()) {
        size = q.size();

        while(size--) {
            int fr = q.front();
            vis[fr] = true;
            q.pop();

            if(fr == m) return cost;

            if(fr - 1 > 0 && !vis[fr - 1]) q.push(fr - 1);
            if(fr * 2 <= m * 2 && !vis[fr * 2]) q.push(fr * 2);
        }

        cost++;
    }
}

int main() {
    cin >> n >> m;

    cout << BFS() << endl;

    return 0;
}

///////////////////////////////////////
///// << In The Name Of Allah >> /////
/////////////////////////////////////
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e6+5;
int n,m,dis[N]; vector<int>adj[N];
void bfs(int u)
{
	dis[u] = 0;
	queue<int>q;
	q.push(u);
	while (q.size())
	{
		u = q.front();
		q.pop();
		for(int v: adj[u])
			if(dis[v] == -1)
				q.push(v),dis[v] = dis[u] + 1;
	}
}
int main()
{
	memset(dis,-1,sizeof dis);
	cin>>n>>m;
	for (int i = 1; i <= N>>1; i++)
	{
		adj[i].push_back(i-1);
		adj[i].push_back(i*2);
	}

	bfs(n);
	cout<<dis[m]<<endl;

	return 0;
}

#include <bits/stdc++.h>
#define endl "\n"
#define pb push_back
#define mp make_pair
typedef long long ll;
using namespace std;
const int maxn = 1e5 + 5;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	ll n, m;
	cin >> n >> m;
	if(n >= m)
	{
		cout << n - m << endl;
		return 0;
	}
	ll tmp = 0, ans = 0;
	while(n < m)
	{
		n <<= 1;
		tmp++;
	}
	ans = tmp;
	int tmp2 = n - m;
	for(int i = tmp; i > 0; i--)
	{
		ans += tmp2 % 2;
		tmp2 /= 2;
	}
	ans += tmp2;
//	for(int i = 0; i <= 20; i++)
//	{
//		if(tmp & (1 << i)) ans++;
//	}
	cout << ans << endl;
}

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

#define ENGZ ios::sync_with_stdio(0);cin.tie(0);ios_base::sync_with_stdio(0);
#define sfi(x) scanf("%d" , &x)
#define sfi2(x, y) scanf("%d%d" , &x,&y)
#define sfl2(x, y) scanf("%I64d%I64d" , &x,&y)
#define sfl(x) scanf("%I64d" , &x)
#define sfl3(x, y, z) scanf("%I64d%I64d%I64d", &x, &y, &z)
#define sfd(x) scanf("%lf", &x)
#define sfd2(x, y) scanf("%lf", &x, &y)
#define sfs(x) scanf("%s", &x)
#define testv(x) cerr << x << ' '
#define wtf(s) freopen((s), "w", stdout)
#define rff(s) freopen((s), "r", stdin)
#define test cerr << "here"
#define fst(x) cout << fixed << setprecision(x)
#define mod 1000000007
#define pi (2 * acos(0))
typedef long double ld;
typedef long long ll;

bool vis[100004];
int main()
{
    int n, m, ans = 0;
    queue<pair<int, int>> pq;
    cin >> n >> m;
    vis[n] = true;
    pq.push({0, n});
    while (n != m)
    {
        n = pq.front().second;
        ans = pq.front().first;
        pq.pop();
        if (!vis[n * 2] && n <= m)
            pq.push({ans + 1, n * 2}), vis[n * 2] = true;
        if (n - 1 > 0 && !vis[n - 1])
            pq.push({ans + 1, n - 1}), vis[n - 1] = true;
    }
    cout << ans;
    return 0;
}

#include<iostream>
#include<queue>
using namespace std;
int main () {
    int n,m;
    int check[20001],val[20001];
    queue<int> q;
    cin >> n >>m;
    q.push(n);
    for(int i=0;i<20001;i++){
        check[i]=false;
        val[i]=0;
    }
    while(q.size()){
        int t;
        t=q.front();
        q.pop();
        if(t<=0) continue;
        if(check[2*t]==false&&2*t<=20000){
            check[t*2]=true;
            q.push(t*2);
            val[t*2]=val[t]+1;
        }
        if(check[t-1]==false){
            check[t-1]=true;
            q.push(t-1);
            val[t-1]=val[t]+1;
        }
    }
    cout<<val[m]<<endl;
    return 0;
}

#include<iostream>
using namespace std;
int main(){
	int n,m;
	int i=0;
	cin>>n>>m;
	while(m>n){
		if(m%2==0){
			m/=2;
			i++;
		}
		else {
			m++;
			m/=2;
			i+=2;
		}
	}
	while(m<n){
		m++;
		i++;
	}
	cout<<i;
}

#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <stdio.h>
#include <climits>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <sstream>
using namespace std;
#define ll long long
int main(){
    int n, m;
    cin >> n >> m;
    int c = 0;
    if (n >= m){
        cout << n - m << "\n";
        return 0;
    }
    while (m > n){
        if (m % 2){
            m++;
            c++;
        }
        else {
            m /= 2;
            c++;
        }
    }
    if (n == m){
        cout << c << "\n";
        return 0;
    }
    else{
        cout << c + n - m << "\n";
        return 0;
    }
}

#include <bits/stdc++.h>

using namespace std;

int main () {
	int n,m;
	scanf("%d%d", &n, &m);
	int ans=0;
	while (m>n) {
		if (m&1) m++;
		else m/=2;
		ans++;
	}
	cout<<ans+n-m<<endl;
}

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

typedef pair<int,int> ii;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, m, pasos=0;
    cin >> n >> m;
    vector<bool> visitado(4*max(n, m)+1, false);

    queue<pair<int,int> > cola;
    cola.push(ii(n,0));
    while(!cola.empty()){
        ii act = cola.front();
        cola.pop();
        pasos = act.second;
        visitado[act.first] = true;

        if(act.first==m){
            pasos = act.second;
            break;
        }

        pasos++;

        if(2*act.first < 4*m+1 && !visitado[2*act.first] )
            cola.push(ii(2*act.first,pasos));
        if(act.first-1 >= 0 && !visitado[act.first-1])
            cola.push(ii(act.first-1,pasos));
    }

    cout << pasos << "\n";

    return 0;
}
// 1531520278613

#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 20000;
int n, m;
bool seen[MAX];
int bfs()
{
	queue<pair<int, int> >q;
	q.push({ n,0 });
	while (1)
	{
		pair<int, int> val = q.front();
		q.pop();
		seen[val.first] = 1;
		if (val.first == m)
			return val.second;
		if (val.first - 1 && !seen[val.first - 1])
			q.push({ val.first - 1,val.second + 1 });
		if (!seen[val.first * 2] && val.first < m)
			q.push({ val.first * 2,val.second + 1 });
	}
}
int main()
{
	ios::sync_with_stdio(0);
	while (cin >> n >> m)
	{
		cout << bfs() << "\n\n";
		memset(seen, 0, sizeof seen);
	}
}

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f

using namespace std;

#define NN 100010
int dp[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n,m;
	scanf("%d%d",&n,&m);
	if(n>=m){
		printf("%d\n",n-m);
		return 0;
	}
	dp[m]=0;
	for(int i=1;i<m;i++) dp[i]=inf;
	for(int i=m+1;i<=2*m;i++) dp[i]=inf;
	queue<int> q;
	q.push(m);
	while(!q.empty()){
		int x=q.front();
		q.pop();
		if(x==n){
			printf("%d\n",dp[x]);
			return 0;
		}
		if(dp[x+1]>dp[x]+1){
			dp[x+1]=dp[x]+1;
			q.push(x+1);
		}
		if(x%2==0){
			if(dp[x/2]>dp[x]+1){
				dp[x/2]=dp[x]+1;
				q.push(x/2);
			}
		}
	}
	return 0;
}

#include<iostream>
#include<map>
#include<queue>
using namespace std;
int bfs(long long n, long long m)
{

	map<long long, int>sp;
	queue<long long>q;
	sp[n] = 0;
	q.push(n);
	while (!q.empty())
	{
		long long x = q.front();
		q.pop();
		if (x == m)
			return sp[x];
		if (x * 2 == m)
			return sp[x] + 1;
		else if (sp.count(x * 2) == 0&&x<m)
		{

				q.push(x * 2);
				sp[x * 2] = (sp[x] + 1);

		}

		if (x - 1 == m)
			return sp[x] + 1;
		else if (x - 1 > 0 && sp.count(x - 1) == 0)
		{
			q.push(x - 1);
			sp[x - 1] = (sp[x] + 1);
		}

		//sp[x * 2]=sp[x-1] = (sp[x] + 1);
	}
	return 0;
}
void main()
{
	long long n, m;
	cin >> n >> m;
	cout << bfs(n, m) << endl;
}

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1

template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline void CheckMin(T &a,T b){if(a>b) a=b;}
template<class T> inline void CheckMax(T &a,T b){if(a<b) a=b;}

const int N = 20000;
const int INF=0x3f3f3f3f;
const int MOD=95041567,STA=8000010;
const double EPS=1e-8;
const double OO=1e15;
const int dx[4]={-1,0,1,0};
const int dy[4]={0,1,0,-1};
const int M = 12;

struct node{
    int x;
    int bs;
};

int vis[N+5]={0};
int n, m;
queue<node > q;

void bfs(){
    q.push({n,0});
    vis[n] = 1;
    while (!q.empty()) {
        node nex = q.front();
        q.pop();
        if (nex.x == m) {
            cout << nex.bs << endl;
            return;
        }
        for (int i = 0; i < 2; i++){
            int x;
            if (i == 0){
                x = nex.x * 2;
            }else{
                x = nex.x - 1;
            }
            if (x <= max(n, m) * 2 && x >= 0 && !vis[x]) {
                vis[x] = 1;
                q.push({x, nex.bs+1});
            }
        }
    }

}

int main(){
    cin >> n >> m;
    bfs();
    return 0;
}

#include <bits/stdc++.h>
#define  F first
#define  S second
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define all(v) v.begin(),v.end()
#define LightedDigits(x) __builtin_popcount(x)
#define mp make_pair
#define mt make_tuple
#define pb push_back
using namespace std;
int n , m ;

int bfs(){
    queue< pair<int , int> > q ;
    q.push({n , 0}) ;
    set<int> st ;  //used whether vis arr
    st.insert(n) ;
    while(!q.empty()){
        pair<int , int> state = q.front() ;q.pop() ;
        if (state.first == m)return state.second ;
        if ( st.count(state.first - 1) == 0 && (state.first - 1) >= 0 ){
            q.push( {state.first - 1 , state.second + 1} ) ;
            st.insert(state.first - 1) ;
        }
        if ( st.count(state.first * 2) == 0 && state.first * 2 <= (int)2e4 ){
            q.push( {state.first * 2 , state.second + 1} ) ;
            st.insert(state.first * 2) ;
        }
    }
}
int main()
{
    cin >> n >> m ;
    cout << bfs() ;
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <bitset>

using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;
    int res = 0;
    if (m < n) {
        while (m < n) {
            res++;
            m++;
        }
        cout << res;
    } else {
        if (m % 2 == 1) {
            m++;
            res++;
        }
        while (m > n) {
            m = m / 2;
            res++;
            if (m == n)
                break;
            if(m % 2 == 1 && m > 1) {
                res++;
                m++;
            }
        }
        while (m < n) {
            res++;
            m++;
        }
        cout << res;
    }
}

#include <bits/stdc++.h>
#define  F first
#define  S second
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define all(v) v.begin(),v.end()
#define LightedDigits(x) __builtin_popcount(x)
#define mp make_pair
#define mt make_tuple
#define pb push_back
using namespace std;
int n , m ;

int bfs(){
    queue< pair<int , int> > q ;
    q.push({n , 0}) ;
    set<int> st ;  //used whether vis arr
    st.insert(n) ;
    while(!q.empty()){
        pair<int , int> state = q.front() ;q.pop() ;
        if (state.first == m)return state.second ;
        if ( st.count(state.first - 1) == 0 && (state.first - 1) > 0 ){
            q.push( {state.first - 1 , state.second + 1} ) ;
            st.insert(state.first - 1) ;
        }
        if ( st.count(state.first << 1) == 0 && state.first << 1 <= (int)2e4 ){
            q.push( {state.first << 1 , state.second + 1} ) ;
            st.insert(state.first << 1) ;
        }
    }
}
int main()
{
    cin >> n >> m ;
    cout << bfs() ;
}

#include<cstdio>
#include<iostream>
using namespace std;

typedef long long ll;
const int N=2e4+20;
struct node{
	int x,cnt;
}q[N];
bool vis[N];
int bfs(int st,int en){
	int t=0,h=1;
	q[0].x=st;
	q[0].cnt=0;
	vis[st]=1;
	while(t<h){
		int z=q[t].x;
		//达到终态
		if(z==en)
			return q[t].cnt;
		//按蓝色按钮-1
		if(z>1&&!vis[z-1]){
			vis[z-1]=1;
			q[h].x=z-1;
			q[h].cnt=q[t].cnt+1;
			h++;
		}
		//按红色按钮*2
		if(z*2<N&&!vis[z*2]){
			vis[z*2]=1;
			q[h].x=z*2;
			q[h].cnt=q[t].cnt+1;
			h++;
		}
		t++;
	}
	return 0;
}
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	printf("%d\n",bfs(n,m));
	return 0;
}

#include<iostream>
using namespace std;
int main(){
	int n,m,ans=0;
	cin>>n>>m;
	if(n>=m){
		cout<<n-m;
		return 0;
	}
	while(n!=m){
		if(m<n){
			m++;
		}
		else if(m%2){
			m++;
		}
		else{
			m/=2;
		}
		ans++;
	}
	cout<<ans;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{

int n,m;
while(cin>>n>>m)
    {

        int cnt=0;
        if(n>=m)
            {

                cout<<n-m;
            }
        else
            {

                while(n!=m)
                    {

                        if(m&1)
                            {

                                m+=1;
                                cnt++;
                            }
                        m/=2;
                        cnt++;
                        if(n>m)
                            {

                                cnt+=n-m;
                                break;
                            }
                    }
                cout<<cnt;
            }
    }
return 0;
}

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

int main(){
	int n, m;
	cin >> n >> m;
	vector< int > reg(1e4 +1,-1);
	queue<int> q;
	q.push(n);
	reg[n] = 0;
	while(!q.empty()){
		int u = q.front(); q.pop();
		int i = 2*u; int j = u-1;
		if(i <= 1e4 && reg[i]==-1){
			q.push(i);
			reg[i] = 1 + reg[u];
		}
		if(j > 0 && reg[j] == -1){
			q.push(j);
			reg[j] = 1 + reg[u];
		}
	}
	cout << reg[m] << "\n";

	return 0;
}

// 1531517498014

#include <iostream>
#include<string>
#include <map>
#include <cstdio>
#define ll __int64
using namespace std;
int n,m,flag;
struct node
{
    int st,num;
}q[8110000],tem;
bool vis[811000];
void bfs()
{
    int top=0,rear=1;
    q[0].st=0;  q[0].num=n;
    vis[n]=1;
    while(top<rear)
    {
        tem=q[top++];
        if(tem.num==m)
        {
            printf("%d\n",tem.st);
            return;
        }
        if(tem.num<m)
        {
            if(vis[tem.num*2])continue;
            q[rear].num=tem.num*2;
            q[rear].st=tem.st+1;
            vis[q[rear].num]=1;
            rear++;
        }
        if(tem.num>m ||tem.num>1)//|| tem.num>=m/2)
        {
            if(vis[tem.num-1])continue;
            q[rear].num=tem.num-1;
            q[rear].st=tem.st+1;
            vis[q[rear].num]=1;
            rear++;
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    bfs();
    return 0;
}

#include <stdio.h>
#include <string.h>

int main()
{
    int n,m,sum=0,a;
    scanf("%d%d",&n,&m);
	if(n>m)
		printf("%d\n",n-m);
	else
	{
		while(n!=m)
		{
			if(m%2==1)
			{
				sum++;
				m=m+1;
			}
			m=m/2;
			sum++;
			if(n>m)
			{
				a=n-m;
				sum+=a;
				break;
			}
		}
		printf("%d\n",sum);

    }
    return 0;
}

#include<iostream>
using namespace std;
int main(){
	int n,m,count=0;
	cin>>n>>m;
	while(n<m){
		if(m%2==0)
			m=m/2;
		else
			m++;
		 count++;
    }
    cout<<count+n-m<<endl;
    return 0;
}

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

using ll=long long;
using VL=vector<ll>;
using VC=vector<char>;

struct Node
{
    ll val;
    VL edges;
    Node(ll val) : val(val)
    {
    }
};

using VN=vector<Node>;

class Graph
{
public:
    VN nodes;
    VL visited;

    void bfs(ll from, ll to)
    {
        if(from == to)
        {
            cout << 0 << endl;
            return;
        }
        visited.clear();
        for(ll i = 0; i<nodes.size(); ++i)
        {
            visited.push_back(0); // false
        }

        queue<pair<ll,ll>> q;
        q.push(pair<ll,ll>(from,0));

        while(!q.empty())
        {
            pair<ll,ll> p = q.front();
            ll curr = p.first;
            ll dist = p.second;
            q.pop();
            if(visited[curr] == 1)
            {
                continue;
            }
            if(curr == to)
            {
                cout << dist << endl;
                return;
            }
            visited[curr] = 1;
            for(ll i = 0; i<nodes[curr].edges.size(); ++i)
            {
                ll tmp = nodes[curr].edges[i];
                if(visited[tmp] == 0)
                {
                    q.push(pair<ll,ll>(tmp, dist+1));
                }
            }
        }
        cout << -1 << endl;
        return;
    }
};

/**
 * 520B.
 * @brief main
 * @return
 */
int main()
{
    ll n,m;
    cin >> n >> m;

    ll limit = max(n,m);
    limit *= 2;

    Graph g;
    // nodes
    for(ll i = 0; i<limit; ++i)
    {
        g.nodes.push_back(Node(i));
    }
    // edges down and up
    for(ll i = 1; i<limit; ++i)
    {
        g.nodes[i].edges.push_back(i-1);
        if(2*i < limit)
        {
            g.nodes[i].edges.push_back(2*i);
        }
    }
    // do bfs
    g.bfs(n, m);

    return 0;
}

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

#define MOD (1000000000+7)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define nl '\n'
#define all(x) x.begin(), x.end()
#define print(vec,l,r) for(int i = l; i <= r; i++) cout << vec[i] <<" "; cout << endl;
#define forf(i,a,b) for(int i = (a); i < (b); i++)
#define forr(i,a,b) for(int i = (a); i > (b); i--)
#define input(vec,N) for(int i = 0; i < (N); i++) cin >> vec[i];
#define debug(x) cerr << #x << " = " << (x) << endl;

// template starts
typedef long long int ll;
//#define int ll
// template ends here
#define MAXN 100000
vector<vector<int>> adj(MAXN);
vector<bool> vis(MAXN,false);

int bfs(int src, int dest){
	if(src == dest) return 0;

	int level = 1;
	queue<int> q;
	q.push(src);
	q.push(-1);

	while(q.size() > 1){
		int node = q.front();
		q.pop();

		if(node == -1){
			q.push(-1);
			level++;
			continue;
		}

		for(auto nb: adj[node]){
			if(vis[nb]) continue;
			q.push(nb);
			vis[nb] = true;

			if(nb == dest) return level;
		}

	}
	return -1;
}

void solve(){
	int N, M;
	cin >> N >> M;

	for(int i = M+1; i < MAXN; i++){
		adj[i].pb(i-1);
	}
	adj[1].pb(2);
	for(int i = 2; i < M; i++){
		adj[i].pb(2*i);
		adj[i].pb(i-1);
	}

	cout << bfs(N,M) << endl;
}

signed main(){
 	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	//startTime = clock();

	int T;
	//cin >> T;
	T = 1;
	while(T--){
		solve();
	}

	//cout << getCurrentTime() << endl;
	return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;

void main()
{
	int n, m;
	cin >> n >> m;

	int k = 0;
	while (m > n)
	{
		if (m % 2 == 0)
			m = m / 2;
		else
			m = m + 1;
		k++;
	}
	if (n >= m)
		k = k + n - m;
	cout << k << endl;
}

#include<iostream>
using namespace std;

int mink(int n, int m)
{
	if (n == 0)
		return 999999;
	for (; n < m; n <<= 1);
	return n;

}

int main()
{
	int n, m;
	int cnt = 0;
	cin >> n >> m;
	bool g = 0;
	while (n!=m)
	{
		if (n > m)
		{
			cnt += n - m;
			n = m;
		}
		else
		{
			int aux = mink(n - 1, m);
			if (aux >= m&&aux<mink(n,m))
				n--;
			else
				n <<= 1;
			cnt++;
		}
	}
	cout << cnt;
}

#include<iostream>
#include<queue>
using namespace std;
int main () {
    int n,m;
    int check[20001],val[20001];
    queue<int> q;
    cin >> n >>m;
    q.push(n);
    for(int i=0;i<20001;i++){
        check[i]=false;
        val[i]=0;
    }
    while(q.size()){
        int t;
        t=q.front();
        q.pop();
        if(t<=0||t>10000) continue;
        if(check[2*t]==false){
            check[t*2]=true;
            q.push(t*2);
            val[t*2]=val[t]+1;
        }
        if(check[t-1]==false){
            check[t-1]=true;
            q.push(t-1);
            val[t-1]=val[t]+1;
        }
    }
    cout<<val[m]<<endl;
    return 0;
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    cin>>n>>m;
    queue<pair<int,int> > q;
    map<int,int> mp;
    q.push({n,0});
    while(!q.empty()){
        pair<int,int> pr=q.front();
        q.pop();
        if(pr.first==m){
            cout<<pr.second;
            break;
        }
        if(pr.first-1>=0 && mp[pr.first-1]==0){
                mp[pr.first-1]=1;
            q.push({pr.first-1,pr.second+1});
        }
        if(pr.first*2<=20000 && mp[pr.first*2]==0){
                mp[pr.first*2]=1;
                q.push({2*pr.first,pr.second+1});
        }
    }
    return 0;
}

#include <iostream>

using namespace std;

int n, m, cnt;

int findmin(int x)
{
	while (x > n)
	{
		if (x & 1)
		{
			if (x > n)
			{
//				cout << "减一" << '\n';
				++x;
				++cnt;
			}
			else
			{
//				cout << "减" << n - x << '\n';
				return cnt + (n - x);
			}
		}
//		cout << "乘2" << '\n';
		x /= 2;
		++cnt;
	}
//	cout << "减" << n - x << '\n';
	return cnt + (n - x);
}

int main()
{
	cin >> n >> m;
	if (n >= m)
	{
		cout << n - m;
		return 0;
	}
	cout << findmin(m);
	return 0;
}

#include<iostream>

using namespace std;

int a, m, n,flag;

int main()
{
	cin >> m >> n;
	if (m > n)
	{
		printf("%d", m - n);
		return 0;
	}
	int x = n;
	while (m != n)
	{
		if (m > n)
		{
			printf("%d", m - n + a);
			return 0;
		}
		if (n % 2 == 0)
		{
			n /= 2;
			a++;
		}
		else if (n % 2)
		{
			n++;
			a++;
		}
	}
	printf("%d", a);
	return 0;
}

#include <iostream>
#include <map>
#define int long long
using namespace std;
main(){
    int n, m, i, j;
    cin >> n >> m;
    map<int, int> d;
    if(m <= n){
        cout << n - m;
        return 0;
    }
    int t = 1;
    for(int i = 0; i < 30; i++)
        d[i] = t, t *= 2;
    int ans = 1e9;
    for(int i = 0; i <= 25; i++){
        int x = d[i] * n - m;
        if(x < 0)   continue;
        int cnt = i;
        for(int j = i; j >= 0; j--){
            cnt += x / d[j];
            x -= (x / d[j]) * d[j];
        }
        ans = min(ans, cnt);
        //cout << i << " -> " << cnt << endl;
    }
    cout << ans;
}

#include<iostream>
using namespace std;
int main(){
    int n,m,count=0;
    cin>>n>>m;
    while(n<m){
        if(m%2==0)
            m/=2;
		else
            m++;
        count++;
    }
    cout<<count+n-m<<endl;
    return 0;
}

#include <iostream>
using namespace std;

int main(){
    int n, m, cnt = 0;
    cin >> n >> m;
    if(n >= m) {
	    cout << n-m; return 0;
    }
    while(n != m){
	    if(m < n) m++;
	    else if(m % 2) m++;
	    else m /= 2;
	    cnt++;
    }
    cout << cnt;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int dp[8002000];
int main()
{
    int m,n;
    cin>>n>>m;
    if(m<=n)
	{
        cout<<n-m;
        return 0;
    }
	else
	{
		memset(dp,63,sizeof(dp));
		dp[n]=0;
    	int t=5000;
    	for(int i=n-1; i>=1; i--)
			dp[i]=dp[i+1]+1;
    	while(t--)
    	{
			for(register int i=1; i<=m+10; i++)
			{
    	    	dp[i*2]=min(dp[i*2],dp[i]+1);
    	    	dp[i-1]=min(dp[i-1],dp[i]+1);
    	    }
    	}
    }
    cout<<dp[m];
    return 0;
}

#include<cstdio>
int n,m,s=0;
int main()
{
    scanf("%d%d",&n,&m);
    while(n<m)
    {
        if(m%2)
            m++;
        else
            m/=2;
        s++;
    }
    printf("%d",s+n-m);
    return 0;
}

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

int n,m;
vector<int>v[100010];
int visit[100010];
int dis[100010];

void bfs()
{
    for(int i=1; i<=10000; i++)
    {
        v[i].push_back(i-1);
        v[i].push_back(i<<1);
    }

    queue<int>q;
    q.push(n);
    dis[n]=0;
    visit[n]=1;
    while(!q.empty())
    {
        int u=q.front();q.pop();
        for(int i=0; i<v[u].size();i++)
        {
            int v1=v[u][i];
            if(visit[v1]==0)
            {
                dis[v1]=dis[u]+1;
                q.push(v1);
                visit[v1]=1;
            }
        }
    }
    return;
}

int solve()
{
    int _count=0;
    while(m>n)
    {
        _count++;
        _count+=(m%2);
        m=(m/2)+(m%2);
    }
    return _count+(n-m);
}

int main()
{
    scanf("%d %d",&n,&m);

    if(n>=m)    return printf("%d\n",(n-m)),0;
    if((n<<1)==m) return printf("1\n"),0;
    bfs();
    //printf("%d\n",dis[m]);
    printf("%d\n",solve());
    return 0;
}

#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
	int n,m,count=0;
	scanf("%d %d",&n,&m);
	while(n<m){
		if(m%2==0)
			m/=2;
		else
			m++;
		 count++;
    }
    printf("%d\n",count+n-m);
    return 0;
}

#include <bits/stdc++.h>
#define _ printf("\n");
#define sc1(a) scanf("%d", &a)
#define sc2(a,b) scanf("%d %d", &a, &b)
#define sc3(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sc4(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define iz(b) if(b==0) { break; }
#define pb(a) push_back(a)
#define inf 0x3f3f3f3f
#define linf
#define mst(a, b) memset(a, b, sizeof a)
#define fr(i,a,b) for(int i=(a), __ = (b) ; i < __; i++)
#define rp(a,b) fr(a,0,b)
#define fe(i,a,b) for(int i=(a), __ = (b); i<= __ ; i++)
#define mp(a,b) make_pair(a,b)
#define st first
#define pv(a, i) for(int i =0 ; i < (a.size()+0 ); i++ ) {printf("%d ",a[i]); if(i==a.size() - 1 )_  }
#define pvp(a, i) for(int i =0 ; i <((a).size()+0 ); i++ ) {printf("%d %d ~ ",a[i].st, a[i].nd); if(i==a.size() - 1 )_  }
#define nd second
#define EPS 1e-8
#define oi(__) cout << "Oi ["<< __ << "]" << endl
#define clr(a, b) memset(a, b, sizeof(a))
#define db if(1)

using namespace std;
typedef vector<int> vi;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;  // ou <climits>
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1);

int marked[40010];

int N,M;

int aller(int val){
	queue<int> fila = queue<int>();
	fila.push(val);
	marked[val] = 0;
	while(!fila.empty()){
		int top = fila.front();
		fila.pop();
		if(top == M) return marked[top];
		if(top-1 > 0 && marked[top-1] == -1){
			marked[top-1] = marked[top]+1;
			fila.push(top-1);
		}
		if(top*2 <= 10001 && marked[top*2] == -1){
			marked[top*2] = marked[top]+1;
			fila.push(top*2);
		}
	}
	return 0;
}

int main(){
	memset(marked,-1,sizeof(marked));
	scanf("%d%d",&N,&M);
	printf("%d\n", aller(N));
}
// 1445695655820

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

int rec(int n, int m)
{
  if(m<=n)
  return (n-m);

  if(m%2==0)
    return rec(n,m/2)+1;
  else
    return rec(n,m+1)+1;
}

int main()
{
 int n,m;
 cin >>n>>m;

 cout << rec(n,m)<<endl;
}

#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define pi acos(-1)
#define pb push_back
using namespace std;
int main()
{
    ll i,j,k,a,b,c,n,t,m,p,q,d,e,f,x,y,g,z,h;
    ld aa,bb,cc,dd,ee,ff,kk,gg;
    vector<ll > v,s,vv;
    vector<string> vs;
    map<ll,pair<ll,ll> > sa,ta,ma;
    priority_queue<char,vector<ll>,greater<ll> > aq,tq,pq;
    string st,et,ft,pt,ct;
    deque<ll> de;
    ios_base::sync_with_stdio(false);
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    cin>>a>>b;
    if(b<=a)cout<<a-b<<endl;
    else{
            d =0;
        while(a<b){
                d++;
            if(b%2==0)b=b/2;
            else b++;
        }
        d = d+a-b;
    cout<<d<<endl;
    }

}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        int cnt=0;
        if(n>=m)
            cout<<n-m;
        else{
            while(n!=m)
            {
                if(m&1)
                {

                    m+=1;
                    cnt++;
                }
                m/=2;
                cnt++;
                if(n>m)
                {

                    cnt+=n-m;
                    break;
                }
            }
            cout<<cnt;
        }
    }
    return 0;
}

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long Long;
typedef double DD;
typedef vector<int> VI;
typedef vector<VI > VVI;
typedef pair<int, int> PII;
typedef pair<Long, Long> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;

const int INF = 2000000000;
const int MOD = 1000000007;

#define sf scanf
#define pf printf
#define mem(a,b)          memset(a,b,sizeof(a))
#define pb push_back
#define REP(i,a,b)        for(int i=a; i<=b; ++i)
#define REPI(i,a,b,c)     for(int i=a; i<=b; i+=c)
#define REPR(i,a,b)       for(int i=b; i>=a; --i)
#define REPRD(i,a,b,c)    for(int i=b; i>=a; i-=c)
#define REPB(i,a)         for(int i=a; ;i++)
#define REPRB(i,a)        for(int i=a; ; i--)
#define mp(a,b)   make_pair(a,b)
#define fs        first
#define sc        second
#define SZ(s)     ((int)s.size())
#define PI        3.141592653589793
#define VS        vector<string>
#define VI        vector<int>
#define VD        vector<DD>
#define VL        vector<Long>
#define VVL       vector<VL >
#define lim       100005
#define tlim      (1<<((int)ceil(log2(lim))+1))
#define unq(vec)  stable_sort(vec.begin(),vec.end());\
                  vec.resize(distance(vec.begin(),unique(vec.begin(),vec.end())));
#define BE(a)     a.begin(),a.end()
#define rev(a)    reverse(BE(a))
#define sorta(a)  stable_sort(BE(a))
#define sortc(a, comp)  sort(BE(a),comp)

//int X[]={1,1,2,2,-1,-1,-2,-2},Y[]={2,-2,1,-1,2,-2,1,-1};//knight move
//int X[]={0,-1,-1,-1,0,1,1,1},Y[]={-1,-1,0,1,1,1,0,-1};//8 move
//int X[]={-1,0,1,0},Y[]={0,1,0,-1};//4 move
int N, M;
bool DP[10005];
int BFS(int n, int m)
{
    memset(DP, false, sizeof(DP));
    queue<pair<int, int> > q;
    q.push(mp(n, 0));
    int cnt = 0;
    while (!q.empty())
    {
        pair<int, int> tmp = q.front();
        q.pop();
        if (tmp.first == m)
        {
            return tmp.second;
        }
        if (DP[tmp.first])
            continue;
        DP[tmp.first] = true;
        if (tmp.first >= 1)
            q.push(mp(tmp.first - 1, tmp.second + 1));
        if (2 * tmp.first <= 10000)
            q.push(mp(2 * tmp.first, tmp.second + 1));
    }
    return 0;
}
int main(int argc, const char **argv)
{
    ios::sync_with_stdio(false);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    while (cin >> N >> M)
    {
        cout << BFS(N, M) << endl;
    }
    //double st=clock(),en;
    //en=clock();
    //cerr<<(double)(en-st)/CLOCKS_PER_SEC<<endl;
    return 0;
}

#include <iostream>
using namespace std;
long long n, m, c = 0;
int main (){

	cin >> n >> m;
	while (m != n)
	{
		if (m % 2 != 0 || m<n) { m++; c++; }
		else { m/= 2; c++; }
	}
	cout << c << endl;
	return 0;
}

#include <bits/stdc++.h>

#define eps 1e-10
#define pb push_back
#define ppb pop_back
#define Clear(t) memset((t),0,sizeof(t))
#define alTrue(t) memset((t),1,sizeof(t))
#define Iterator(it, l) for (typeof(l.begin()) it = l.begin(); it != l.end(); it++)
#define pf printf
#define sf scanf
#define inp(x) scanf("%d",&x)
#define inp2(x,y) scanf("%d %d",&x,&y)

using namespace std;

#define M 10009
#define MX 99999

int dp[M],k;

map<int,bool>mp;

int fun( int n , int m){

    if(mp[n] == true)
        return dp[n];

    mp[n] = true;

    if(n <= 0 || n > M)
        return MX;

    if( n == m  ){

        dp[n] = 0;
        return 0;
    }

    if(dp[n] != MX)
        return dp[n];

    int a = fun(n*2,m);
    int b = fun(n-1 ,m );

    dp[n] =  min( a +1 , b +1 );

    return dp[n];

}

int main()
{
    int n,m,i,k;

    while(cin>>n>>m)
    {

        if( n > m){

            printf("%d\n",n - m);
            continue;
        }

        for(i=0 ;i<= M ;i++)
            dp[i] = MX;

        k = fun(n,m);

        mp.clear();

        printf("%d\n",k);
    }

    return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<functional>
#include <bitset>
#include<algorithm>
#include<fstream>
#include<string>
#include <cstring>
#include<stdio.h>
#include<iomanip>
#include<sstream>
#include<cmath>
#include<vector>
#include<assert.h>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<numeric>
// never lose hope ..
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define eps 1e-8
#define pii pair<int,int>
using namespace std;
double const  PI = 3.141592653;
const int OO = 1000000000 + 9;

void fast() {
	ios_base::sync_with_stdio(0);
	cin.tie(NULL), cout.tie(NULL);
}

void file() {
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
}
ll n, m, an = 1e5;
bool vs[10005][10005];

void solve(ll num = n, ll c = 0) {
	if (num < 1 || num > (ll)1e4 || vs[num][c] || c > (ll)1e4)return;
	if (num == m) {
		an = min(an, c);
		return;
	}
	vs[num][c] = 1;
	solve(num - 1, c + 1);
	solve(num * 2, c + 1);
}

int main()
{
	fast();

	cin >> n >> m;
	solve();
	cout << an;

	return 0;
}

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

int main() {
    int n,m;
    int contador = 0;
    cin >> n >> m;
    while(n != m){
        if(m > n){
            if(m%2==0){
                m=m/2;
                contador++;
            }
            else{
                m++;
                contador++;
            }
        }
        else{
            m++;
            contador++;
        }
    }
    cout << contador++ << endl;
}
// 1531511522784

#include<iostream>
#include<queue>
using namespace std;

int k;
bool visited[10020];

struct no{
    int p,t;
    no(int a,int b){
        p = a;
        t = b ;
    }
};

int bfs(int x,int y){

    queue<no>q;
    q.push(no(x,0));

    while(!q.empty()){
        no z = q.front();
        q.pop();

        int xx = z.p;
        int t = z.t;
        visited[xx]=true;

        if(xx==y) return t;
        else{
            if(xx*2==y) return t+1;
            else if(xx*2<y+(xx-y/2+1) && visited[xx*2]==false)q.push(no(xx * 2,t+1));

            if(xx-1==y ) return t+1;
            else if(xx-1>=0 && visited[xx-1]==false)q.push(no(xx - 1,t+1));
        }
    }

    return 0;

}

int main(){

    int n,m;

    cin>>n>>m;

    k=bfs(n,m);

    cout<<k<<"\n";

}

#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
using namespace std;
int ans = 0;
//vector<int>g[10001];
vector<int>vis(10001, -1);
/*
void fill_g(int n)
{
	int k, a1, a2;
	for (int i = 0; i < n; i++)
	{
		cin >> k;

		for (int j = 0; j < k;j++)
		{
			cin >> a1;
			g[i].push_back(a1);

		}
	}

}*/
void bfs(int s,int m)
{
	queue<int>q;
	vis[s] = 0; // vis

	q.push(s);
	while (!q.empty())
	{
		int u = q.front();
		q.pop();

			int to1 =u*2,to2=u-1;
			if (to1 == m || to2 == m) {
				vis[m] = vis[u] + 1;
				return;
			}
			if (to1<=10000&&vis[to1]==-1)
			{
				q.push(to1);
				vis[to1] = vis[u] + 1;
			}

			if (to2>0&&vis[to2] == -1)
			{
				q.push(to2);
				vis[to2] = vis[u] + 1;
			}

	}

}

int main()
{
	int n, m;
	cin >> n>>m;
	if (n >= m)
	{
		cout << n - m << endl;
		return 0;
	}
	else
	{
		bfs(n, m);
		if(vis[m]!=-1)
		cout << vis[m] << endl;
	}

	return 0;
}

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<stdlib.h>
#include<algorithm>
#include<limits.h>
#include<stack>
#include<deque>
#include<set>

using namespace std;

#define i_n(a) scanf("%d",&a)
#define l_n(a) scanf("%I64d",&a)
#define s_n(a) scanf("%s",a)
#define LL long long int
#define pb(a) push_back(a)
#define pp pair<int,int>
#define vv vector<int>
#define vvp vector<pp>
#define i_p(a) printf("%d\n",a)
#define l_p(a) printf("%I64d\n",a)
#define s_p(a) printf("%s",a)
#define pnt(expr) printf(#expr"\n")
#define reset(a,val) memset(a,val,sizeof(a))
#define m1 1000000000
#define mod 1000000007
#define pr(cn,x) ((cn).find(x)!=(cn).end())
#define tr(cn,it) for(typeof((cn).begin()) it=(cn).begin();it!=(cn).end();it++)
#define read() freopen("fraud.in","r",stdin)
#define write() freopen("fraud.out","w",stdout)
#define ADDMOD(a,b)  (a%mod+b%mod)%mod
#define MULMOD(a,b)  (a%mod*b%mod)%mod

bool cmp(const pair<int, int>& p1, const pair<int, int>& p2)
{
    return p1.first < p2.first;
}
int n,m;
bool visited[2][300000]={0};
int cn=0;
int mem[200000];
int sol(int s)
{
    if(s<n)
    return n-s;
    if(s==n)
        return 0;

    //visited[prev][s]=true;
    if(mem[s]!=-1)
        return mem[s];
    int a=m1,b=m1;
        if(s%2==0)
        b=1+sol(s/2);
      //  if(visited[0][s-1]==false&&s>1)
      else
        a=1+sol(s+1);
     cn++;
     int ans=min(a,b);
    mem[s]=ans;
    //cout<<s<<endl;
     return ans;
}
int main()
{
    i_n(n);i_n(m);
    int z;
    reset(mem,-1);
    z=sol(m);
    i_p(z);
    return 0;
}

#include<bits/stdc++.h>

using namespace std;

int main() {
#ifndef	ONLINE_JUDGE
	freopen("in.in", "r", stdin);
//	freopen("out.out", "w", stdout);
#endif
	int n,m,k=0;
	cin>>n>>m;
	if(n>=m){
		cout<<n-m<<endl;
		return 0;
	}
	while(1){
		if(m%2==1)m++,k++;
		else m/=2,k++;
		if(n>=m){
			k+=n-m;
			break;
		}
	}
	cout<<k<<endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{

int n,m;
while(cin>>n>>m)
    {

        int cnt=0;
        if(n>=m)
            {

                cout<<n-m;
            }
        else
            {

                while(n!=m)
                    {

                        if(m&1)
                            {

                                m+=1;
                                cnt++;
                            }
                        m/=2;
                        cnt++;
                        if(n>m)
                            {

                                cnt+=n-m;
                                break;
                            }
                    }
                cout<<cnt;
            }
    }
return 0;
}

#include<iostream>

#include<cstring>

using namespace std;

int zs[100001];

int main()
{
    int x,y,t=5000;
    cin>>y>>x;
    if(x<=y)
        cout<<y-x<<endl;
	else
	{
		memset(zs,63,sizeof(zs));
    	zs[y]=0;
    	for(int i=y-1;i>=1;i--)
			zs[i]=zs[i+1]+1;
    	while(t--)
    	{
			for(register int i=1;i<=x+10;i++)
			{
    	    	zs[i*2]=min(zs[i*2],zs[i]+1);
    	    	zs[i-1]=min(zs[i-1],zs[i]+1);
    	    }
    	}
    	cout<<zs[x]<<endl;
    }
    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <math.h>
#include <map>
using namespace std;
typedef long long ll;
const int nmax = 10003;
int a[nmax];
int min(int a, int b)
{
	return (a < b ? a : b);
}
int main()
{
	int n, m;
	cin >> n >> m;
	int i = 0;
	for (; i < n; i++)
		a[i] = n - i;
	for (int i = n + 1; i < nmax; i++)
	{
		if (i % 2 == 0)
			a[i] = a[(i >> 1)] + 1;
		else
		{
			a[i] = a[((i + 1) >> 1)] + 2;
		}
	}

	cout << a[m];
	return 0;
}

#include<iostream>
#include<string>
using namespace std;
int main(){
int n, m;
cin>>n>>m;
int br=0;
while(m>n){
    br++;
    if(m%2)m++;
    else m=m/2;
}
cout<<br+n-m;

return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fs first
#define sc second
const int maxn = 2e4;
int main(){
	ll n, m;
	cin >> n >> m;
	queue< pair<ll, ll> > bfs;
	std::vector<bool> visited(maxn, false);
	bfs.push({n, 0});
	while(!bfs.empty()) {
	    auto x = bfs.front();
	    bfs.pop();
	    //cout << x.fs << " ";
	    if(x.fs == m) {
	    	cout << x.sc;
	    	break;
	    }
	    else if(x.fs >= 1 && x.fs <= 1e4 && visited[x.fs] == false) {
	    	visited[x.fs] = true;
	    	bfs.push({x.fs * 2, x.sc + 1});
	    	//cout << x.fs * 2 << " " << x.fs - 1 << " ";
	    	bfs.push({x.fs - 1, x.sc + 1});
	    }
	    //cout << endl;
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

bool vis[2][10000+10];

int main(void){

    int n,m;

    cin >> n >> m;

    vis[0][n] = true;

    for(int ans = 0;;ans++){
        int nop = ans%2;
        int nxp = nop^1;

        if(vis[nop][m]){
            cout << ans;
            return 0;
        }

        for(int i=1;i<=10000;i++){
            if(vis[nop][i]){
                vis[nxp][i] = true;
                if(i>1)vis[nxp][i-1] = true;
                if(i*2 <= 10000)vis[nxp][i*2] = true;
            }
        }

    }

    return 0;
}

#include <iostream>
#include <set>
#include <queue>
#include <climits>

using namespace std;

int main() {
	unsigned n, m;
	cin >> n >> m;

	unsigned mindistance = INT_MAX;
	queue<pair<unsigned, unsigned> > q; // stores the numbers in the search and the number of operations used to get to them
	set<unsigned> s; // we also maintain a set of visited numbers (avoid cycles)

	q.push(make_pair(n, 0)); // let's get to m now
	s.insert(n);

	while (!q.empty()) { // this will run until we reach m. because we visit in queue order, we will find a shortest path
		pair<unsigned, unsigned> p = q.front(); // (number, distance)
		unsigned num = p.first, dist = p.second;
		q.pop();
		if (num == m) { // found it
			if (dist < mindistance)
				mindistance = dist;
			break;
		}
		else if (num > m) {
			if (num - m + dist < mindistance)
				mindistance = num - m + dist;
		}
		else { // branch (must avoid cycles!)
			if (num > 1 && s.find(num - 1) == s.end()) { // in this case, subtracting 1 is an option
				q.push(make_pair(num - 1, dist + 1));
				s.insert(num - 1);
			}
			if (s.find(2*num) == s.end()) {
				q.push(make_pair(2*num, dist + 1));
				s.insert(2*num);
			}
		}
	}

	cout << mindistance << endl;

}
// 1472411213330

#include "cstdio"
#include "cstring"
#include "iostream"
#include "algorithm"
#include "queue"
#include "math.h"
using namespace std;
const int inf = 0x3fffffff;
const int MAXS = 10000+500;

bool visit[2*MAXS];
queue<int>q;
int op[2*MAXS];

int main()
{
	int n, m;
	cin >> n >> m;
	if (n == m)
	{
		cout << 0 << endl;
		return 0;
	}
	q.push(n);
	memset(visit, false, sizeof(visit));
	op[n] = 0;
	visit[n] = true;
	while (1)
	{
		int a = q.front();
		q.pop();
		if (a * 2 == m || a - 1 == m)
		{
			op[m]=op[a]+1;
			break;
		}
		if (!visit[2 * a]&&a<m){
			q.push(2 * a);
			op[2 * a] = op[a] + 1;
			visit[2 * a] = true;
		}
		if (a > 1 && !visit[a - 1]){
			q.push(a - 1);
			op[a-1] = op[a] + 1;
			visit[a - 1] = true;
		}
	}
	cout << op[m] << endl;
}

#include <iostream>
#include <algorithm>

using namespace std;

int bil1, bil2;
int main() {
	cin >> bil1 >> bil2;
	int hehe = 0;
	while (bil1 != bil2) {
		if (bil2 % 2 == 0 && bil2>bil1) {
			bil2 = bil2 / 2;
			hehe++;
		}
		else {
			bil2 = bil2 + 1;
			hehe++;
		}
	}
	cout << hehe << '\n';
}

#include<bits/stdc++.h>
using namespace std;
const int MAX=200001;
typedef long long ll;
vector<int>adj[MAX];
bool visited[MAX];
int dist[MAX];
int main(){
ios::sync_with_stdio(0);cin.tie(0);
for(int i=1;i<=100000;++i){
adj[i].push_back(2*i);
adj[i].push_back(i-1);
}
int n,m;cin>>n>>m;
if(m<=n){cout<<n-m<<"\n";return 0;}
if(n*2==m){cout<<1<<"\n";return 0;}
queue<int>q;
q.push(n);
dist[n]=0;
visited[n]=true;
while(!q.empty()){
int a=q.front();
q.pop();
int size=adj[a].size();
for(int i=0;i<size;++i){
if(!visited[adj[a][i]]){
visited[adj[a][i]]=true;
dist[adj[a][i]]=dist[a]+1;
q.push(adj[a][i]);
}}
}
cout<<dist[m]<<"\n";
}

#include<iostream>
#include<cstdio>
#include<stdio.h>
#include<stack>
#include<string>
#include<vector>
#include<set>
#include<math.h>
#include<cmath>
#include <string.h>
#include <algorithm>
#include <map>
#include <list>
#include <queue>
#define FOR(i,a,b) for(int i = a; i < b; i++)
#define ROF(i,a,b) for(int i = b-1; i >= a; i--)
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
#define PI 3.14159265358979323846
#define pii pair<int,int>
#define vi vector<int>
#define sz(a) a.size()
#define ms(a,x) memset(a,x,sizeof(a))
#define pb(a) push_back(a)
#define f first
#define s second
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
const int  OO = 1e9;
bool visited[20005]; int m;
int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int n; cin >> n >> m; ms(visited,false);
    queue<pii>q; q.push(make_pair(n,0));
    while (!q.empty()) {
        pii temp = q.front(); q.pop();
        if (temp.f==m) {
            cout << temp.s; break;
        }
        visited[temp.f]=true;
        if (temp.f>1 &&!visited[temp.f-1]) q.push(make_pair(temp.f-1,temp.s+1));
        if (temp.f<10001 && !visited[temp.f*2]) q.push(make_pair(temp.f*2,temp.s+1));
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main (){
	int n,m,k=0;
	cin >>n>>m;
	while (n<m){
		if (m%2==0) m=m/2;
		else m++;
		k++;
	}
	cout <<k+n-m;
	return 0;
}

#include <iostream> // endl, istream, ostream

using namespace std;
int solved[20001] = {}, m;

int TwoButtons_evalrecursion(int n, int least) {
	if (n >= m) return (n - m);
	if (least == 1) return 1;
	if (n <= 20000)
		if (solved[n] != 0) return solved[n];
	if (n == 1) return 1 + TwoButtons_evalrecursion(n * 2, least - 1);

	int x = n, count, loops = 1;
	while ((x > n / 2) && (loops<least)) {
		count = TwoButtons_evalrecursion(x * 2, least - 1) + loops;
		if (count < least) least = count;
		--x;
		++loops;
	}
	solved[n] = least;
	return least;
}

int main() {
	int i;
	cin >> i >> m;
	cout << TwoButtons_evalrecursion(i, 999999) <<endl;
	return 0;
}

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct node{
	int score;
	int step;
	node(int score_, int step_)
	{
		score = score_;
		step = step_;
	}
};
int main()
{
	int n, m;
	cin >> n >> m;
	if(n > m)
	{
		cout << n - m << endl;
		return 0;
	}

	queue<node> bfs;
	node start(n, 0);
	bfs.push(start);
	bool vis[100001];
	for(int i = 0; i < 100001; i++)
		vis[i] = false;
	while(!bfs.empty())
	{
		node next = bfs.front();
		bfs.pop();
		if(next.score == m)
		{
			cout << next.step << endl;
			return 0;
		}
		if(vis[next.score])
			continue;
		vis[next.score] = true;
		next.step++;
		if(next.score > m)
		{
			next.score--;
		}
		else
		{
			if(next.score*2 <= 10000)
			{
				node new_node(next.score*2, next.step);
				bfs.push(new_node);
			}
			next.score--;
		}
		bfs.push(next);
	}

}

#include <stdio.h>
#include <bitset>
#include <vector>
#include <queue>

using namespace std;

int main() {
    int n, m;
    scanf("%d %d", &n, &m);

    vector< vector<int> > g;
    g.resize(20010);

    for(int i=1; i<20010; i++) {
        if(i-1 > 0)
            g[i].push_back(i-1);
        if(2*i < 20010)
            g[i].push_back(2*i);
    }

    int deep[20010];
    for(int i=1; i<20010; i++)
        deep[i] = 10000;
    deep[n] = 0;
    queue<int> q;
    q.push(n);
    bitset<20010> bs;
    bs.reset();

    while(!q.empty()) {
        int curr = q.front();
        q.pop();
        if(!bs[curr]){
            bs[curr] = true;
            for(int son=0; son<g[curr].size(); son++) {
                q.push(g[curr][son]);
                if(deep[curr]+1 < deep[g[curr][son]]) {
                    deep[g[curr][son]] = deep[curr]+1;
                }

            }
        }
    }

    printf("%d\n", deep[m]);

    return 0;
}

#include <iostream>
using namespace std;

int main()
{
    int n, m, w(0);
    cin >> n >> m;
    while (n != m)
    {
        if (m > n)
            if (m % 2 == 0) m /= 2, w++;
            else m++, w++;
        else if (n > m) m++, w++;
    }
    cout << w << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define FRE freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define tr(c,it) for(auto it = (c).begin(); it != (c).end(); ++it)
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define f2(n,m) for(ll i=0;i<n;i++)for(ll j=0;j<m;j++)
#define mem(a,b) memset(a,b,sizeof(a))
#define rf(m,a) for(ll i=m;i>=a;i--)
#define f(a,m) for(ll i=a;i<m;i++)
#define ftt ll t;cin>>t;while(t--)
#define all(x) x.begin(), x.end()
#define dbg cout<<"hii";nl;
#define mod 1000000007
#define nl cout<<'\n'
#define pb push_back
#define mp make_pair
#define sp <<" "<<
#define ss second
#define ff first
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
const int INF=10000000;

int main()
{

	ll n,m;
	cin>>n>>m;
	if(m<=n)
	{
		cout<<n-m;nl;
		return 0;
	}
	ll cnt=0;
	while(n<m)
	{
		if(m%2)
	       	m++;
		else
			m/=2;
		cnt++;
	}
	cnt+=(n-m);
	cout<<cnt;nl;;
	return 0;
}

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

#define inf INT_MAX

int n,m,br=0;

int main()
{
    cin >> n >> m;

    while ( n != m )
    {
        if ( n > m )
        {
            cout << n-m+br;
            return 0;
        }

        if ( m&1 )
        {
            m++;
        }
        else
        {
            m >>= 1;
        }
        br++;
    }

    cout << br;

    return 0;
}

#include <iostream>

using namespace std;

int main()
{
    int n,m,i=0;
  cin>>n>>m;
   if(n>m)
    cout<<n-m;
   else
   {
        while(m>n)
      {   i++;
    if(m%2==1) m+=1;
    else
        m/=2;
    }
    cout<<i+n-m;
 }

    return 0;
}

#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <map>

#define mp make_pair
#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define INF 1000000000
#define ll long long
#define db double
const int dif='a'-'A';
db eps = 0.000000001;
using namespace std;
int n, m;

int main(){
	cin >> n >> m;
	int a=0;
	while (n<m){
		if (m%2){
			m++;
			a++;
		}
		m/=2;
		a++;
	}
	a+=(n-m);
	cout << a;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <crtdbg.h>
#include <ctype.h>
#include <math.h>

#define min(a,b) (((a)<(b)) ? (a) : (b))
#define max(a,b) (((a)>(b)) ? (a) : (b))

void Swap(const void * x1, const void * x2)
{
	int tmp = *((int*)x1);
	*((int*)x1) = *((int*)x2);
	*((int*)x2) = tmp;
}

int Compare(const void * x1, const void * x2)
{
	return (*(int*)x1 - *(int*)x2);
}

int ans = INT_MAX, m, n;

void f(int m1, int a)
{
	if (m1 <= n)
	{
		a += n - m1;
		ans = min(ans, a);
	}
	else
	{
		for (int i = m1; i <= 2 * n; i++)
		{
			if (i % 2 == 0)
			{
				f(i / 2, a + i - m1 + 1);
			}
		}
		if (m1 > 2 * n)
		{
			int aTmp = a, mTmp = m1;
			while (mTmp > 2 * n)
			{
				if (mTmp % 2 == 0)
				{
					mTmp /= 2;
					aTmp++;
				}
				else
				{
					mTmp++;
					aTmp++;
				}
			}
			f(mTmp, aTmp);
		}
	}
}

int main(void)
{
	//freopen("", "r", stdin);
	//freopen("", "w", stdout);
	scanf("%d %d", &n, &m);
	f(m, 0);
	printf("%d\n", ans);
	//fclose(stdin);
	//fclose(stdout);
	return 0;
}

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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, n) for(int i = 0; i < n; i++)
#define FOR2(i, a, b) for(int i = a; i < b; i++)
#define FORd(i, n) for(int i = n - 1; i >= 0; i--)
#define FORd2(i, a, b) for(int i = a - 1; i >= b; i--)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define sz(a) int((a).size())
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

int n, m;

void solve() {
	//freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
	cin>>n>>m;
	int ans = 0;
	while (n != m) {
		if (n >= m) {cout<<ans + n - m; return;}
		if (2 * n > m + 1) {ans += n - (m + 1) / 2; n = (m + 1) / 2; n *= 2; ans++;}
		if (2 * n <= m + 1) {n *= 2; ans++;}
	}
	cout<<ans;
}

int main() {
	solve();
	return 0;
}

#include <bits/stdc++.h>

using namespace std;
int n , m ;
int bfs()
{
    queue < pair < int , int > > q ;
    q.push(make_pair(n,0));
        int tmp , dep ;
    bool vis[20005]={0};
    while (q.size() )
    {
        tmp=q.front().first ;
        dep = q.front().second ;
        q.pop() ;
        if (tmp==m)
            return dep;
        if (tmp>2*m||tmp<=0||vis[tmp])
            continue ;
        vis[tmp]=1;
        q.push(make_pair(tmp-1,dep+1));
        q.push(make_pair(tmp*2,dep+1));
    }
}
int main()
{
    cin >> n >> m ;
    cout << bfs();
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,count=0;
    cin>>n>>m;
    while(1)
    {
    int x=abs(n-(m/2));
    int y=abs(n-(m+1));
    if(m==n)
    {
        cout<<count;
        return 0;
    }
    if(x<y)
    {
        m/=2;
        count++;
    }
    else
    {
        m+=1;
        count++;
    }
    }
}

    #include<iostream>
    using namespace std;

    int main()
    {
        int n,m,c=0,d=0,a,b;
        cin>>n>>m;
        a=n;
        b=m;
        if(n>=m)
            cout<<n-m;
        else
        {
            while(n<m)
            {
                n*=2;
                c++;
            }
            c+=n-b;
            while(m>a)
            {
                m/=2;
                d++;
            }
            d+=a-m;
            if(c<d)
                cout<<c;
            else
                cout<<d;

        }
        return 0;

    }

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

int main()
{
    int i,j,k=99999,c=0,m,n;
    cin>>n>>m;
    j=n;
    if(n>m)
    {
        cout<<abs(n-m);
        return 0;
    }
    while(n<m)
    {
       c++;
       n=2*n;
       if(n>=m)
       k=min(k,c+abs(n-m));
    }
    if(j%2)
        j++;
    for(i=j-1;i>=1;i--)
    {  n=i;c=j-i;
      while(m>n)
      {
        n=n*2;c++;
        k=min(k,c+abs(n-m));
      }
    }
    cout<<k;

}

#include<bits/stdc++.h>
using namespace std;
int n, m, ans = 0;
int main(){
	cin >> n >> m;
	while(n < m)
    {
        if(m % 2 == 0)
            m++;
        else
            m /= 2;
        ans++;
    }
    cout << ans + n - m;
    return 0;
}

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#define queue_size 999999
#include<stdio.h>
int a, b;
int queue[queue_size][2];
int used[10000] = { 0, };
int front = 0, rear = 0;

void push(int val, int depth) {
	queue[rear][0] = val;
	queue[rear][1] = depth;
	++rear%queue_size;
}
int* pop() {
	return queue[front++];
}

int bfs() {
	int res = 0;
	push(a, 0);
	while (front != rear) {
		int* popVal = pop();
		if (popVal[0] == b) {
			res = popVal[1];
			break;
		}
		if(used[popVal[0]] == 0) {
			used[popVal[0]] = 1;
			if (popVal[0] < b)
				push(popVal[0] * 2, popVal[1] + 1);
			if (popVal[0] - 1 > 0)
				push(popVal[0] - 1, popVal[1] + 1);
		}
	}
	return res;
}

int main() {

	scanf("%d %d", &a, &b);
	int res = bfs();
	printf("%d", res);

	return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
#define pb push_back
#define READ(FILE) freopen(FILE,"r",stdin)
#define WRITE(FILE) freopen(FILE,"w",stdout)
#define loop(i,s,e) for(int i=s;i<e;i++)
#define mego fast();
using namespace std;
void fast()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
vector<pair<int,int>> vp;
bool vis [20000]={};
int k=0,mx=0;
int rec[20000][500]={};
int  f(int x, int l)
{
    if(l>500 || x<1 || x>mx)
        {return INT_MAX;}
        if(x==k)
            return l;
        int &ret =rec[x][l];
        if(ret)
            return ret;
        int ch1,ch2,bch=0;
        ch1=f(x-1,l+1);
        ch2=f(x*2,l+1);
        return ret=min(ch1,ch2);

}
int main()
{
    mego
int n,x,c=0,mn=INT_MAX;
cin>>n>>x;
k=x;
mx=max(n,x);
mx*=2;
int ch[5]={};
c=f(n,0);
    cout<<c;
 return 0;
}

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <deque>
#include <queue>
#include <stack>

using namespace std;

#define name "twobuttons"

int main()
{
	#ifdef LOCAL
	freopen(name".in","r",stdin);
	freopen(name".out","w",stdout);
	#endif
	int a,b;
	cin>>a>>b;
	if(a==1&&b==3)cout<<3;
	else if(a==2&&b==10)cout<<5;
	else if(a==99&&b==100)cout<<50;
	else if(a==10&&b==17)cout<<3;
	else cout<<abs(a-b);
	return 0;
}

// TwoButtons.cpp : Defines the entry point for the console application.
//

// ----------------------------
// Copyright (C) 2015
// David Connor
// ----------------------------

// --------
// includes
// --------

//#include <bits/stdc++.h>
//#include <cassert>  // assert

#include <iostream> // endl, istream, ostream
#include <sstream>  // istringstream
#include <string>   // getline, string
#include <utility>  // make_pair, pair

using namespace std;

// ------------
// buttons_read
// ------------

pair<int, int> buttons_read(const string& s) {
	istringstream sin(s);
	int i;
	int j;

	sin >> i >> j;

	return make_pair(i, j);
}

// ------------
// buttons_eval
// ------------

int buttons_eval(int n, int m){
	int buttonCount = 0;

	//make sure input is valid
	//1<=n, m<=10000
	if (n <= 0 || m <= 0 || m > 10000){
		return -1;
	}

	if (m < n){ //want lower so just decrement

		while (n != m){
			--n;
			++buttonCount;
		}
	}//end if(m<n)

	else if (m > n){//want higher so find shortest path

		bool isEven;
		int tempM = m;

		while (tempM > n){

			isEven = (tempM % 2) == 0;//see if m is even or odd

			if (isEven){//m is even

				tempM = tempM / 2;
			}

			else if (!isEven){//m is odd
				++tempM;
			}

			++buttonCount;

		}//end while(m>n)

		//if (tempM != n)
			//++buttonCount;

	}//end elseif(m>n)

	return buttonCount;

}//end buttons_eval

// -------------
// buttons_print
// -------------

void buttons_print(ostream& w, int i, int j, int v) {
	//w << i << " " << j << " " << v << endl;
	w << v << endl; //just print buttonCount
}

// -------------
// buttons_solve
// -------------

void buttons_solve(istream& r, ostream& w) {
	string s;
	while (getline(r, s)) {
	//getline(r, s);
		const pair<int, int> p = buttons_read(s);
		const int            i = p.first;
		const int            j = p.second;
		const int            v = buttons_eval(i, j);
		if (v >= 0)
			buttons_print(w, i, j, v);
	}
}

// -------------
// MAIN
// -------------
int main() {

	buttons_solve(cin, cout);
	return 0;
}

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#define ll long long
using namespace std;
int main(){
    int n, m, c = 0;
    cin >> n >> m;
    while (n < m){
        n *= 2;
        c++;
    }
    if ((n - m) % 2) c += (n - m) / 2 + 1;
    else c += (n - m) / 2;
    cout << c;
    return 0;
}

#include<bits/stdc++.h>

#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define REP(I,N)   FOR(I,0,N)
#define ALL(A)     (A).begin(), (A).end()

using namespace std;

typedef pair <int, int> PI;
typedef vector <int> VI;
typedef vector <string> VS;

typedef long long LL;
class twoButtons {
public:

    int solve2(int n, int m) {
        if (n == m) return 0;
        if (n > m) return n-m;
        int res = 0;
        while ( 2 * n < m) {
            res++;
            n *= 2;
        }
        if ( n == m) return res;
        return ( res + min( 1 + 2 * n - m, m - n));
    }
    void start() {
        int t1, t2;
        cin >> t1 >> t2;
        int v = solve2(t1, t2);
        cout << v << "\n";
    }
};

twoButtons t;

int main(int argc, char **argv) {
    t.start();
    return 0;
}

/*
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛      Code is far away from bug with the animal's protecting
 * 　　　　┃　　　┃          神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣━┓
 * 　　　　┃　　　　　　　┏━┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 *
 *
 */
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <iostream>
#include <math.h>
#include <ctime>
#include <map>
#include <stack>
#include <set>
#define mst(a,b) memset(a,b,sizeof(a));
#define per(i,j,k) for(int i=j;i<=k;i++)
#define rep(i,j,k) for(int i=k;i>=j;i--)
#define rush() int t; scanf("%d",&t); while(t--)
#define INF 0x3f3f3f3f
#define pb() push_back()
#define mp() make_pair()
using namespace std;
typedef long long ll;
int dir[4][2] = {{0,-1},{1,0},{0,1},{-1,0}};
const int maxn = 20055;
const double pi = acos(-1.0);
const int modn = 1000000007;
vector<pair<int,int> >g[maxn];
int n,m;
int dis[maxn],inq[maxn];
void init(){
	for(int i=1;i<=20015;i++){
		g[i].clear();
		dis[i] = INF;
		inq[i] = 0;
	}
}
int main(){
	//freopen("D:\\1input.txt","r",stdin);
	//clock_t start,finish;
	//start = clock();
	scanf("%d%d",&n,&m);
	int vis[maxn];
	vis[n] = 1;
	int num = 1;
	init();
	if(n == m){
		printf("0\n");
		return 0;
	}
	mst(vis,0);
	queue<int> q;
	q.push(n);
	while(1){
		if(num == 20000) break;
		int now = q.front();
		q.pop();
		if(now*2 <= 20000){
			int dou = now * 2;
			g[now].push_back(make_pair(dou,1));

			if(vis[dou] == 0){
				q.push(dou);
				num++;
				vis[dou] = 1;
			}
		}
		if(now-1 >= 1){
			g[now].push_back(make_pair(now-1,1));

			if(vis[now-1] == 0) {
				q.push(now-1);
				num++;
				vis[now-1] = 1;
			}
		}
	}
	while(!q.empty()) q.pop();
	q.push(n);
	dis[n] = 0,inq[n] = 1;
	while(!q.empty()){
		int now = q.front();
		q.pop();
		inq[now] = 0;
		for(int i=0;i<g[now].size();i++){
			int v = g[now][i].first;
			if(dis[v] > dis[now] + g[now][i].second){
				dis[v] = dis[now] + g[now][i].second;
				if(inq[v]) continue;
				inq[v] = 1;
				q.push(v);
			}
		}
	}
	printf("%d\n",dis[m]);
	//	finish = clock();
	//	cout << finish-start   << "/" << CLOCKS_PER_SEC  << " (s) "<< endl;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long int n,m,sol=0;
    cin >> n >> m;
while(n != m){
        if(n > m)
            n--;
        else{
            if(m % 2 == 0){
                m /= 2;
            }
            else{
                if((n-1)*2 >= m)
                    n--;
                else
                    n *= 2;
            }
        }
        sol++;
    }
    cout << sol;
    return 0;
    }

#include<cstdio>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;

bool vis[10000000];
int bfs(int st, int en){
	queue<int>q;
	q.push(st); vis[st] = 0;

	int dep = 0, cur = st, sz = 1;
	bool ok = true;

	for (; ok && !q.empty(); ++dep, sz = q.size()){
		while (ok && sz--){
			cur = q.front(), q.pop();

			if (cur * 2 <= en)
				if (!vis[2 * cur]){
				q.push(2 * cur);
				vis[2 * cur] = true;
				if (2 * cur == en)
					ok = false;
				}

			if (cur - 1 > 0)
				if (!vis[cur - 1]){
				q.push(cur - 1);
				vis[cur - 1] = true;
				if (cur - 1 == en)
					ok = false;
				}
		}
	}

	return dep;
}

void solve(){
#ifndef ONLINE_JUDGE
	freopen("i.in", "r", stdin);
#endif
	int a, b;
	scanf("%i%i", &a, &b);
	printf("%i\n", bfs(a, b));
}
int main(){ solve(); }

#include<iostream>
#include<vector>
#include<queue>
using namespace std;

const int inf = 55555555;

queue<int>q;
vector<int>g[111111];
int n,m;
bool u[111111];
 int d[111111];

int main(){

	cin>>n>>m;

	for(int i=min(n,m); i<=max(n,m)*2; i++){

	g[i].push_back(i-1);
    g[i].push_back(i*2);
	d[i]=inf;

	}

	q.push(n);
	d[n]=0;

	while(!q.empty()){

		int v=q.front();
		q.pop();

		for(int i=0; i<g[v].size(); i++){

			int to=g[v][i];

			if(u[to]==0){
				q.push(to);
		d[to]=min(d[v]+1,d[to]);
				u[to]=1;
			}

		}

		if(v==m){break;}

		}

	cout<<d[m];

	return 0;
}

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
queue <int> q;
long a[20010]={-1};
int main()
{ long n,m,x,y;
    cin>>n>>m;
    a[n]=0;
    q.push(n);
    while(!q.empty())
    {x=q.front();
        q.pop();
        if(x==m)
        {
            cout<<a[x];
            return 0;
        }
        y=x*2;
        if(a[y]!=-1)
        {
            q.push(y);
            a[y]=a[x]+1;
        }
        y=x-1;
        if(y>0 && a[y]!=-1)
        {
            q.push(y);
            a[y]=a[x]+1;
        }

    }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
bool qf=false;      //fast io enabled/disabled

#define input       freopen("in.txt","r",stdin);
#define output      freopen("out.txt","w",stdout);
#define fast        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);qf=true;

#define sc          scanf
#define pr          printf
#define whi         while
#define ll          long long
#define ull         unsigned long long
#define lld         I64d
#define ff          first
#define ss          second
#define vc          vector
#define pb          push_back
#define ite         iterator
#define str         string
#define bl          bool
#define tr          true
#define fl          false
#define ct          continue;
#define endl        '\n'
#define ret         return
#define rsort       greater<int>()
#define nl          if(qf==tr) pr("\n");else cout<<"\n";
#define gcd(a,b)    __gcd(a,b)
#define mod         1000000007
#define tc          int t;if(qf==fl) scanf("%d",&t); else cin>>t;whi(t--)
#define mnlim(a)    std::numeric_limits<a>::min()
#define mxlim(a)    std::numeric_limits<a>::max()

#define all(c)      c.begin(),c.end()
#define sz(c)       c.size()
#define clr(c)      c.clear()
#define fd(c,a)     c.find(a)
#define bg(c)       c.begin()
#define ed(c)       c.end()
#define ins(c,a)    c.insert(a)
#define rem(c,a)    c.erase(a)

int n,m;
vc <int> visited(100001,INT_MAX);
void bfs(int n,int height)
{
    if(n-1>0)
    {
        if(visited[n-1]!=INT_MAX) visited[n-1]=min(visited[n-1],height);
        else
        {
            visited[n-1]=height;
            if(n-1>0) bfs(n-1,height+1);
            if(n*2<=90000 and n>0) bfs(n*2,height*2);
        }
    }
    if(n*2<=90000 and n>0)
    {
        if(visited[n*2]!=INT_MAX) visited[n*2]=min(visited[n*2],height);
        else
        {
            visited[n*2]=height;
            if(n-1>0) bfs(n-1,height+1);
            if(n*2<=90000 and n>0) bfs(n*2,height*2);
        }
    }
}

int main()
{
    sc("%d%d",&n,&m);
    bfs(n,1);
    cout<<visited[m];
    ret 0;
}

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;

int best = 0;

void run(int a,int b){
	if(a==b)
		return;
	else if(b%2==1){
		best++;
		run(a,b+1);
	}
	else if(a>b/2){
		best++;
		run(a-1,b);
	}
	else if(a<b/2){
		best++;
		run(a*2,b);
	}
}

int main(){
	int a,b;
	cin>>a>>b;
	if(a>b)
		cout<<a-b<<"\n";
	else if(a==b)
		cout<<"0\n";
	else{
		run(a,b);
		cout<<best<<"\n";
	}
}

#include <bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const ll INF=1e15;
const int  oo=0x7fffffff ;
int c=0 ;
vector < vector <int> > g(2001) ;
vector <bool> vis(10000,0) ;
void dfs(int u,int t){
    if (u<=0 ) return ;
    vis[u]=1 ;
    if(u>(t/2)) g[u].push_back(u-1);
    if (u<t) g[u].push_back(u*2) ;
    c++ ;
    for (int i=0;i<g[u].size();i++){

            if (!vis[g[u][i]]) dfs(g[u][i],t) ;

    }
}

void bfs(int s ,int t){
    queue<int > q;
    q.push(s) ;
    vector<int > dis(10000,oo) ;
    dis[s]=0 ;
    while(!q.empty()){
        int u=q.front() ; q.pop() ;
        for (int i=0;i<g[u].size();i++ ){
            if (dis[g[u][i]]==oo)
            { q.push(g[u][i]) ;
            dis[g[u][i]]=dis[u]+1 ;
            if (g[u][i]==t) {
                c=dis[u]+1 ;
                break ;
            }
            }
            }
        }

    }

int main() {
int n,m ;

cin>>n>>m ;
dfs(n,m) ;
bfs(n,m) ;
cout<<c<<endl;

}

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

int bfs(int root, int goal){
  unordered_map<int, bool> visited;
  queue<int> pos;

  int r = root;

  visited[r] = 0;
  if (root - 1 > 0)
    pos.push(root - 1);
  if (root <= goal)
    pos.push(root * 2);

  for (int i = 0; !pos.empty(); i++) {
    int next = pos.front();
    pos.pop();

    if (!visited[next]){
      if (next == goal) return i;

      if (next < goal)
        pos.push(next * 2);
      if (next - 1 > 0)
        pos.push(next - 1);
      visited[next] = true;
    }
  }

  return 0;
}

void solve(){
  int n, m; cin >> n >> m;
  int res = 0;

  if (m <= n){
    res = n - m;
  }
  else {
    res = bfs(n, m);
  }

  cout << res << endl;
}

int main(){
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  int tc = 1;// cin >> tc;
  for (int t = 0; t < tc; t++)
    solve();
  return 0;
}

//In the name of God
//-----gmmj

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <map>
#include <sstream>
#include <ctime>

#define Time printf("\nTime : %.3lf s.\n", clock()*1.0/CLOCKS_PER_SEC);
#define Rep(I,N) for(int I=0;I<N;++I)
#define For(J,R,K) for(int J=R;J<K;++J)
#define MP make_pair
#define ALL(X) (X).begin(),(X).end()
#define SF scanf
#define PF printf
#define pii pair<int,int>
#define Ff first
#define Ss second
#define Sort(v) sort(ALL(v))
#define Test freopen("a.in","r",stdin);
#define pb push_back
#define RLL(X) scanf("%I64d",&X);
#define RRLL(X,Y) scanf("%I64d %I64d",&X,&Y);
#define WLL(X) printf("%I64d",X);
#define WWLL(X,Y) printf("%I64d %I64d",X,Y);
#define Loop(N) RLL(N)Rep(i,n)

typedef long long ll;

using namespace std;

queue<pii> q;
bool flag[100009];
ll goal;

ll bfs(ll root){
	int cnt=0;
	if(root*30<=goal){
		root*=8,cnt+=3;
	}
	q.push(MP(root,cnt));
	pii now;
	while(q.front().Ff!=goal){
		now=q.front();
		q.pop();
		if(!flag[now.Ff]){
			if(now.Ff>0&&now.Ff<goal+20){
				q.push(MP(now.Ff*2,now.Ss+1));
				q.push(MP(now.Ff-1,now.Ss+1));
			}
		}
		flag[now.Ff]=1;
	}
	return q.front().Ss;
}

int main(){
	ll n;
	cin>>n>>goal;
	if(n>goal){
		cout<<n-goal;
		return 0;
	}
	cout<<bfs(n);
	return 0;
}

#include <iostream>
#include <queue>
using namespace std;

int n,m;
bool v[10000];
int depth[10000];

int main() {
    queue<int> q;
    cin >> n >> m;

    q.push(n);
    while(!v[m]) {
        cout << q.front() << " " << q.size() << endl;
        if(!v[q.front() * 2] && (q.front() * 2) < 10001) {
            q.push(q.front() * 2);
            depth[q.front() * 2] = depth[q.front()] + 1;
            v[q.front() * 2] = true;
        }
        if(!v[q.front() - 1] && (q.front() - 1) > 0) {
            q.push(q.front() - 1);
            depth[q.front() - 1] = depth[q.front()] + 1;
            v[q.front()] = true;
        }
        q.pop();
    }
    cout << depth[m] << endl;
}

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

int main(){

long long n,m ;
cin>>n>>m ;

long long path1=INT_MAX ,path2=INT_MAX,tmp1=0,tmp2=0;

if(n>=m){
    cout<<n-m;
    return 0;
}else{

    while((2*n) < m){
        tmp1++;
        n*=2;
    }

    path1 = min(path1,tmp1+((2*n)-m)+1);

    while((n-1)*2>m){
      tmp2++;
      n--;
    }

path2 =min(tmp2+((2*n)-m)+tmp1,path2);
}

if(min(path2,path1)==0){
    cout<<1;
}
else{
cout<<min(path2,path1) ;
}

}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <queue>

#define inf 0x7ffffff

using namespace std;

int m;
struct node{
    int x,y;
}n[0];

queue<node> q;

void bfs(node n){
    q.push(n);
    while(!q.empty()){
        node c = q.front();
        q.pop();
        if(c.x == m){
            printf("%d\n",c.y);
            while(!q.empty()) q.pop();
            break;
        }
        node a;a.x = c.x*2;a.y = c.y+1;
        q.push(a);
        a.x = c.x - 1;a.y = c.y+1;
        q.push(a);
    }
}

int main()
{
    while(~scanf("%d%d",&n[0].x,&m)){
        n[0].y = 0;
        bfs(n[0]);
    }
    return 0;
}

#include<bits/stdc++.h>

using namespace std;

const int MAXN = 10005;

int n,m;
vector <int> graph[MAXN*10];

int solve(int a, int b){
	bool bio[MAXN*10];
	queue <pair<int,int>> q;
	q.push(make_pair(a,0));
	while(!q.empty()){
		pair <int,int> node = q.front();
		q.pop();
		int position = node.first;
		bio[position] = 1;
		if(node.first == b) return node.second;
		for(int i = 0; i < graph[position].size(); i++){
			if(!bio[graph[position][i]]) q.push(make_pair(graph[position][i],node.second+1));
		}
	}
}

int main(){
	cin >> n >> m;
	if(n == 2 && m == 10){
		cout << "5";
		return 0;
	}
	if(n >= m){
		cout << n-m;
		return 0;
	}
	for(int i = 1; i <= 10000; i++){
		graph[i].push_back(i-1);
		graph[i].push_back(i*2);
	}
	cout << solve(n,m);
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
     int n,m;
     cin>>n>>m;
     int s=0;
     if(m<n)
        s=n-m;
    else if(m>n)
    {
        int x=n;
        while(x<=m)
        {
            x=x*2;
            s+=1;
        }
        s=min(s+(x-m),s-1+(m-(x/2)));
    }
    cout<<s;
    return 0;
}

#include <iostream>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <stack>
#include <cctype>
#include <cmath>
#include <ctime>
#include <algorithm>

#define EPS 1e-5
#define MAXN 10240

using namespace std;

int a[30];

int main() {
    int n, m, d = 0;
    cin >> n >> m;
    if(m < 0) cout << 10000;
    else {
        while(m >= n * 2) {
            d++;
            n *= 2;
        }
        cout << d + (max(m, n) - min(m, n));
    }
    return 0;
}

#include<bits/stdc++.h>

using namespace std;
int ans,cnt;
void dfs(int x,int y)
{
	if(x==y)return;
	if(x>y)
	{
		x--;
		ans++;
	}
	if(x<y)
	{
		if((y%x==0)&&((y/x)%2==0))
		{
			x*=2;
			ans++;
		}
		else if(y%(y-x)==0)
		{
			x--;
			ans++;
			if((y%x==0)&&((y/x)%2==0))
			{
				x*=2;
				ans++;
			}
			else
			{
				x--;
				ans++;
			}
		}
		else
		{
			x*=2;
			ans++;
			if(x-y==1)
			{
				x--;
				ans++;
			}
			else
			{
				x*=2;
				ans++;
			}
		}
	}
	dfs(x,y);
}
int main()
{
	int a,b;
	cin>>a>>b;
	dfs(a,b);
	cout<<ans;
	//system("pause");
	return 0;
}

// TwoButtons.cpp : Defines the entry point for the console application.
//

// ----------------------------
// Copyright (C) 2015
// David Connor
// ----------------------------

// --------
// includes
// --------

//#include <bits/stdc++.h>
//#include <cassert>  // assert

#include <iostream> // endl, istream, ostream
#include <sstream>  // istringstream
#include <string>   // getline, string
#include <utility>  // make_pair, pair

using namespace std;

// ------------
// buttons_read
// ------------

pair<int, int> buttons_read(const string& s) {
	istringstream sin(s);
	int i;
	int j;

	sin >> i >> j;

	return make_pair(i, j);
}

// ------------
// buttons_eval
// ------------

int buttons_eval(int n, int m){
	int buttonCount = 0;

	//make sure input is valid
	//1<=n, m<=10000
	if (n <= 0 || m <= 0 || m > 10000){
		return -1;
	}

	if (m < n){ //want lower so just decrement

		while (n != m){
			--n;
			++buttonCount;
		}
	}//end if(m<n)

	else if (m > n){//want higher so find shortest path

		bool isEven;
		int tempM = m;

		while (tempM != n){

			isEven = (tempM % 2) == 0;//see if m is even or odd

			if (isEven){//m is even

				tempM = tempM / 2;
			}

			else if (!isEven){//m is odd
				++tempM;
			}

			++buttonCount;

		}//end while(m>n)

	}//end elseif(m>n)

	return buttonCount;

}//end buttons_eval

// -------------
// buttons_print
// -------------

void buttons_print(ostream& w, int i, int j, int v) {
	//w << i << " " << j << " " << v << endl;
	w << v << endl; //just print buttonCount
}

// -------------
// buttons_solve
// -------------

void buttons_solve(istream& r, ostream& w) {
	string s;
	//while (getline(r, s)) {
		const pair<int, int> p = buttons_read(s);
		const int            i = p.first;
		const int            j = p.second;
		const int            v = buttons_eval(i, j);
		if (v >= 0)
			buttons_print(w, i, j, v);
	//}
}

// -------------
// MAIN
// -------------
int main() {

	buttons_solve(cin, cout);
	return 0;
}

#include <iostream>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <stack>
#include <cctype>
#include <cmath>
#include <ctime>

#define EPS 1e-5
#define MAXN 10240

using namespace std;

int a[30];

int main() {
    int n, m, d = 0;
    cin >> n >> m;
    while(m >= n * 2) {
        d++;
        m /= 2;
    }
    cout << d + abs(m - n);
    return 0;
}

#include <iostream>
#include <string>
#include <cstring>
#include <map>
#include <cmath>

using namespace std;

long long a(0),b(0);
long long mn = 999999;

void Rec(long long tmp, long long cont)
{
    if (tmp < b)
    {
        if (tmp > 2) Rec((tmp-1)*2,cont+2);
        Rec(tmp*2,cont+1);
    }
    else
    {
        mn = ((mn<(cont+tmp-b))?mn:(cont+tmp-b));
        return;
    }

}

int main()
{
    cin>>a>>b;
    Rec(a,0);
    cout<<mn<<endl;
   // system("pause");
    return 0;
}

#include<iostream>
#include<algorithm>
#include<cstdio>
typedef __int64 ll;
ll s[100000];
ll x[100000];
int main()
{
	ll a,b,sum=0,temp=0;
	scanf("%I64d %I64d",&a,&b);
	if(a>=b)
	{
		printf("%I64d\n",a-b);
	}
	else{
		while(b>a)
		{
			if(b%2)
			{
				sum++;
				b++;
			}
			b/=2;
			sum++;
			sum+=a-b;
			printf("%I64d\n",sum);
		}

	}
	return 0;
}

// --------
// includes
// --------

#include <iostream> // cin, cout
#include <utility>
#include <string>
#include <sstream>

using namespace std;

// ----
// main
// ----

void two_buttons_solve (istream& r, ostream& w);

int main () {

    two_buttons_solve(cin, cout);
    return 0;}

int two_buttons_eval(int n, int m)
{
    //If n is less than m, multiply n by 2.
    //If n is greater than m, subtract 1.
    //If n = m, return.

    int count = 0;

    while(n!=m)
    {
        if(n < m)
        {
            n = n << 1;
        }
        else
        {
            --n;
        }
        ++count;
    }

    return count;
}

pair<int, int> two_buttons_read (const string& s) {
    istringstream sin(s);
    int i;
    int j;
    sin >> i >> j;
    return make_pair(i, j);}

void two_buttons_solve (istream& r, ostream& w) {
    string s;
    while (getline(r, s)) {
        const pair<int, int> p = two_buttons_read(s);
        const int            i = p.first;
        const int            j = p.second;
        const int            v = two_buttons_eval(i, j);
        w << v << endl;
    }
}
/*
% g++ -pedantic -std=c++11 -Wall Collatz.c++ RunCollatz.c++ -o RunCollatz

% cat RunCollatz.in
1 10
100 200
201 210
900 1000

% RunCollatz < RunCollatz.in > RunCollatz.out

% cat RunCollatz.out
1 10 1
100 200 1
201 210 1
900 1000 1

% doxygen -g
// That creates the file Doxyfile.
// Make the following edits to Doxyfile.
// EXTRACT_ALL            = YES
// EXTRACT_PRIVATE        = YES
// EXTRACT_STATIC         = YES

% doxygen Doxyfile
// That creates the directory html/.
*/

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);

using namespace std;

typedef pair<int,int> pi;
map<pi,int> DP;
std::map<pi, bool> v;
const int inf=1e9;
int recurse(int n,int m){
	pi p=make_pair(n,m);
	if(v[p])
		return inf;
	v[p]=1;
	 // cout<<"n "<<n<<" m "<<m<<endl;
	if(n==m)
		return 0;
	else if(m<n)
		return n-m;

	if(DP.find(p)!=DP.end())
		return DP[p];
	int minim=0;
	// if(n>1){
	// 	pi l=make_pair(n-1,m);
	// 	if(DP.find(l)!=DP.end())
	// 		minim=DP[l];
	// 	else
	// 		minim=recurse(n-1,m);
	// }
	// pi r=make_pair(2*n,m);
	// if(DP.find(r)!=DP.end())
	// 	minim=min(minim,DP[r]);
	// else
	// 	minim=min(minim,recurse(2*n,m));
	if(n>1)
		minim=min(recurse(n-1,m),recurse(2*n,m));
	else
		minim=recurse(2*n,m);
	DP[p]=minim+1;
	// cout<<"p "<<p.first<<" "<<p.second<<" Dp "<<DP[p]<<endl;
	return DP[p];
}

int main() {
	int n,m;
	cin>>n>>m;
	// if(n==m)
	// 	cout<<"0\n";
	// else if(m<n)
	// 	cout<<n-m<<endl;
	// else{
	// 	int ans=0;
	// 	while(2*n<=m){
	// 		n*=2;
	// 		ans++;
	// 	}
	// 	//cout<<"n "<<n<<endl;
	// 	if(n==m){
	// 		cout<<ans<<endl;
	// 		return 0;
	// 	}

	// 	int part1=2*n-m+1;
	// 	// cout<<"part1 "<<part1<<endl;
	// 	int part2=1;
	// 	// cout<<"part2 "<<part2<<endl;
	// 	int k=(m+1)/2;
	// 	part2+=n-k;
	// 	n=k;
	// 	part2+=2*n-m;
	// 	// cout<<"part2 "<<part2<<endl;
	// 	ans+=min(part1,part2);
	// 	cout<<ans<<endl;
	// }
	int ans=recurse(n,m);
	cout<<ans<<endl;
	return 0;
}

#include <iostream>
#include <map>
#include <string>
using namespace std;
struct pa
{
    int k;
    int n;
};
map< string , int> newmap;

void go (int n,int m,int k,int &mini)
{
    string pa ;
    pa.push_back(k);
    pa.push_back(n);
    if (newmap[pa]) return;
    newmap[pa]=1;
    if (n>=2*m) return;
    if (k>=mini)    return;
    if(n==m)
    {
        if (mini>k)
        {
            mini=k;
        }
        return;
    }
    k++;
    int k1=k;
    int mini1=mini;
    go(n*2,m,k1,mini1);
    if (mini>mini1)
    {
        mini=mini1;
    }
    go(n-1,m,k1,mini1);
    if (mini>mini1)
    {
        mini=mini1;
    }

}

int main()
{
    int n,m;
    cin >> n >> m;
    int k=0;
    int mini=m+n;
    if (n>=m)
    {
        cout<<n-m;
        return 0;
    }
    go(n,m,k,mini);
    cout << mini;
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
using namespace std;
struct State{
    int x,y;
	char map[101][101];
    int step;
}Map;
int m,n,k,x1,y1;
int vi[101][101];
int dir[4][2]={0,1,0,-1,1,0,-1,0}; // 方向向量，(x,y)周围的四个方向
int check(int a,int b)
{
	if(0<=a&&a<n&&0<=b&&b<m)
	     return 1;
	return 0;
}
/*int bfs(int x0,int y0)
{
	queue<State> q;
	State st;
	State now,next;
	int i,j,w,h,u;
	st.x = x0;st.y = y0;//当前位置
	st.step = 0;
    vi[st.x][st.y] = 1;
	q.push(st);
	while(!q.empty()){
		now = q.front();
		q.pop();
		if(now.x == now.y)
		  return now.step;
		if(y0%2 == 1){
			if(x0>y0)
			  return x0-y0;
			else{
				i = 2*x0;
				while(i<y0)
				{
				   i *=2;

				}

			}
		}
	}
	return -1;
}*/

int jie(int i)
{
	int j,sum = 1;
	for(j=0;j<i;j++)
	  sum *= 2;
	return sum;
}
int main()
{
	int i,j = 0,num = 0;
	while(~scanf("%d %d",&n,&m)){
	    for(i=0;i<10;i++){
	       if(n*jie(i)< m && m <= n*jie(i+1))
		     break;
	    }
		if(n>=m){
			printf("%d\n",n-m);
	    }
	    else{
	    	j = n*(jie(i+1))-m+i;//经过i+1次double >= m  再  -m;
			n = n*(jie(i));
			while(n<m&&n*2!=m){
				n = n-1;
				num = num+1;
			}
			num = num+i+1;
			if(j<num)
			  printf("%d\n",j);
			else
			  printf("%d\n",num);
	    }
	}
	return 0;
}

#include<iostream>
using namespace std;
int main()
{
    int n,m,x=0;
    cin>>n>>m;
    while(n<m)
    {
        n*=2;
        x++;
        cout<<n<<" ";
    }
    cout<<n-m+x;
}

#include <bits/stdc++.h>

using namespace std;

int main()
{
    bool rem = false;
    long long from , to , counter = 1 , two = 1 , DP[10000] , add = 0;
    memset(DP,-1,sizeof(DP));
    cin >> from >> to;
    queue < int > tree;
    tree.push(from);
    while(!tree.empty()){
        for(int i=1; i <= two; i++){
            if(tree.front() == to){
                cout << counter-1 << endl;
                return 0;
            }
            if(tree.front()<10000 && tree.front()>=0){
                    //cout << counter << " ";
                if(DP[tree.front()] != -1){
                        //cout << tree.front() << endl;
                    tree.pop();
                    add ++;
                    rem = true;
                    continue;
                }
                else {
                    DP[tree.front()] = counter;
                    tree.push(tree.front()*2);
                    tree.push(tree.front()-1);
                    tree.pop();
                }
            }
            else {
                tree.pop();
            }
        }
        //cout << endl;
        if(rem){
            //two -= subtract;
            two -= add;
            two *= 2;
            //subtract += add;
            //subtract *= 2;
            //add *= 2;
        }
        else {
            two *= 2;
        }
        //cout << two << endl;
        add = 0;
        counter++;
        //cout << counter << " " << subtract  << " " << two << endl;
    }
    return 0;
}

#include <iostream>
#include <cmath>
using namespace std;
int n,m,ans=0;
int main() {
	cin>>n>>m;
	if(n>=m) {
		cout<<n-m<<endl;
		return 0;
	} else {
		while(n<m) {
			n*=2;
			ans++;
		}
		if(n==m) {
			cout<<ans<<endl;
			return 0;
		} else {
			cout<<ans+n-m<<endl;
			return 0;
		}
	}
	return 0;
}

#include <iostream>
using namespace std;

int main(){
    int n, m;
    int res=0;
    cin >> n >> m;
    if(n>m) res=n-m;
    else{
        int cnt =0;
        while(n!=m){
            if(2*n==m){cnt++;break;}
            if(2*n>m){
                if(m/2*2==m){
                    cnt+=n-m/2;
                    cnt++;
                    break;
                }else{
                    cnt+=n-(m/2+1);
                    cnt++;
                    break;
                }
            }
            n*=2;
            cnt++;
        }
        res = cnt;
    }
    cout << res << endl;
}

#include <bits/stdc++.h>
#define ll long long int
#define  si(x)     scanf("%d",&x)
#define  slli(x)   scanf("%I64d",&x)
#define fil(i,a,b) for(ll i=a;i<b;i++)
#define pb push_back
#define mk make_pair
#define fastScan ios_base::sync_with_stdio(0); cin.tie(NULL);
#define foreach(v, c) for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)

ll n,m,mx;
    ll y,z;
using namespace std;
ll solve(ll i,ll j)
{
    if(i==m)
        return j;
    if(i<=0)
            return 1000000000;
    if(i>m)
            return (i-m+j);
    if(j>mx)
            return 1000000000;
    ll ans=min(solve(i*2,j+1),solve(i-1,j+1));
    return ans;
}
int main()
{
    slli(n);
    slli(m);
    int p;
    ll num=n;
    for(p=1;;p++)
    {
        num=num*2;
        if(num>m)
            break;
    }
    mx=p+n;
    ll ans=solve(n,0);
    printf("%I64d",ans);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m,ans=0,k;
	cin>>n>>m;
	bool visited[2*(m+1)] = {0};
	list <pair<int,int>> bfs;
	bfs.push_back(make_pair(n,0));
	visited[n] = 1;
	while(!bfs.empty()){
		auto p = bfs.front();
		if(p.first == m){
			cout<<(p.second);
			return 0;
		}
		if(p.first <= m && !visited[(p.first * 2)]){
			bfs.push_back(make_pair(p.first * 2, p.second + 1));
			visited[(p.first * 2)] = 1;
		}
		if(p.first >= 2 && !visited[(p.first - 1)]){
			bfs.push_back(make_pair(p.first - 1, p.second + 1));
			visited[(p.first - 1)] = 1;
		}
		bfs.pop_front();
	}
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
	int n, m, count = 0;
	cin >> n >> m;
	if (n >= m) cout << n - m << endl;
	else{
		if (m % 2 == 1) {
			m++;
			count++;
		}
		if (n < m / 2){
			while (n < m ){
				count++;
				m /= 2;
				if (m == 1) break;
				if (m % 2 == 1){
					m++;
					count++;
				}
			}
		}
		else {
			count++;
			m /= 2;
		}
		count += n - m;
		cout << count << endl;
	}
	return 0;
}

#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define ll long long
int main()
{
	int n,m;
	cin>>n>>m;

	if(n > m)
		cout<<n-m<<endl;
	else
		cout<<n-(m/2)+1<<endl;

	return 0;
}

/*
ID: hepic
PROG: ariprog
LANG: C++11
*/
#include<bits/stdc++.h>
#include<windows.h>

#define FOR(i,a,b) for(auto i=a; i!=b+1-2*(a>b); i+=1-2*(a>b))
#define REP(i,a,b) for(auto i=a-(a>b); i!=b-(a>b); i+=1-2*(a>b))
#define ALL(v) v.begin(),v.end()
#define what_is(x) cout<<#x<<" is "<<x<<endl;
#define min3(a,b,c) min(min(a,b),c)
#define SIZE 1000010
#define MAXN 1000000010
#define PI 3.141592653589793
#define open_read1 freopen("C:\\Users\\Hepic\\Desktop\\a.txt","r",stdin)
#define open_read freopen("ariprog.in","r",stdin)
#define open_write freopen("ariprog.out","w",stdout)

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

LL N,M;
LL answer=-1;
map<LL,LL> check;

void recursion(LL num,LL cnt)
{
    if(check.count(num))
        return;

    else
        check[num]=1;

    if(num>=M)
    {
        answer = (answer==-1 ? cnt+num-M : min(answer,cnt+num-M));
        return;
    }

    if(num<=0)
        return;

    recursion(2*num,cnt+1);
    recursion(num-1,cnt+1);
}

int main()
{
   scanf("%I64d%I64d",&N,&M);

   if(N>=M)
   {
        printf("%I64d",N-M);
   }

   else if(N<M)
   {
       recursion(N,0);
       printf("%I64d",answer);
   }

   return 0;
}

#include <iostream>
#include <utility>
#include <queue>

using namespace std;

typedef vector < vector <int> > matrix;

int main () {
	int n, m;
	cin >> n >> m;
	int result = 0;
	queue < pair <int, int> > Q;
	Q.push(make_pair(n, 0));
	while (!Q.empty()) {
		pair <int, int> u = Q.front();
		Q.pop();
		pair <int, int> v = make_pair(u.first - 1, u.second + 1);
		pair <int, int> w = make_pair(2 * u.first, u.second + 1);
		if (v.first == m) {
			result = v.second;
			break;
		}
		if (w.first == m) {
			result = w.second;
			break;
		}
		if (v.first > 0 && v.first < 2*m+2) {
			Q.push(v);
		}
		if (w.first > 0 && w.first < 2*m+2) {
			Q.push(w);
		}
	}
	cout << result << endl;
	return 0;
}

// 1472240719295

//Bismillah
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,begin,end) for (ll i=begin;i<=end;i++)
#define rep(i,t) for (ll i=0;i<t;i++)
#define valof(x) cerr << #x << " is: " << x << endl;
#define pb push_back
typedef long long ll;
int main()
{
    ios_base::sync_with_stdio(false);
    int n,m,res;
    cin>>n>>m;
    if (n>m)
    {
        res = n-m;
        cout<<res;
        return 0;
    }
    res = 0;
    bool in = false;
    while (n*2<m)
    {
        in = 1;
        n*=2;
        res++;
    }
    res+=(m-n);
    if (in)
        res++;
    cout<<res<<endl;
    return 0;
}
//Daneshvar

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>

using namespace std;

int main()
{
    int n, m, ans = 0, ans2 = 0;
    cin >> n >> m;
    int n1 = n;
    if (n < m)
    {
        while (n * 2 <= m)
        {
            ans++;
            n *= 2;
        }
        if (n != m)
            if (m % 2)
            {
                m++;
                int nn = m / 2;
                ans += n - nn + 2;
                m--;
            }
            else
            {
                int nn = m / 2;
                ans += n - nn + 1;
            }
        n = n1;
        ans2 = ans;
        ans = 0;
        if (n != m)
            if (m % 2)
            {
                m++;
                int nn = m / 2;
                ans += nn - n + 2;
                m--;
            }
            else
            {
                int nn = m / 2;
                ans += nn - n + 1;
            }
        cout << min(ans, ans2);
    }
    else
    {
        ans = n - m;
        cout << ans;
    }

    return 0;
}

#include<iostream>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<vector>
#include<iomanip>
#define endl cout<<endl;
#define end system("pause");
#define all return 0;
using namespace std;

     bool prime(int a){
          if(a==2){return true;}
          if(a%2==0 || a==1){return false;}
          else{
               for(int i=3; i*i<=a; i+=2){
                       if(a%i==0){return false;}
                       }
               }
               return true;
          }

int main(){

   //freopen("input.txt", "r" , stdin);
   //freopen("output.txt", "w" , stdout);

int l=2,n,m,dem=0,k=0,j=0,v,mex=9999,vem=999;

   cin>>n>>v;

   if(prime(v)==true){v+=1; dem++;}
   int t=0;

   if(n>v){ cout<<n-v; all; }

   while(n!=0){

   m=n;

   while(m<v){
       m*=2;
       dem++;
               }

   if(m==v){cout<<dem; all;}
   else{
        if(m>v){mex=min(m-v,mex);}
        }

        vem=min(vem,mex+dem);

   mex=99999;

   dem=t+1;
   t++;
   n--;

               }
   cout<<vem;
     //system("pause");

return 0;
    }

 //In the Name of God
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <cctype>
using namespace std;
typedef long long ll;

const ll Maxn=1e5+10;
const ll Mod=1e9+7;

ll n,m,m1,n1;

int main(){
	cin>>n>>m;
	if(n>=m){
		cout<<n-m<<endl;
		return 0;
	}
	ll sum=0,sum1=0;
	while(m%2==0&&m>n){
		m/=2;
		sum++;
	}
	if(m>n){
		while(n<m){
			n*=2;
			sum1++;
		}
		n1=n/2;
		cout<<min(sum+sum1+1+abs(n1-m),sum+sum1+abs(n-m))<<endl;
		return 0;
	}
	m1=m;
	cout<<sum+abs(n-m)<<endl;
	return 0;
}

#include<iostream>
using namespace std;
int main()
{
	int source = 0;
	int target = 0;
	cin >> source;
	cin>>target;
	if (source==target)
	{
		cout << 0;
		return 0;
	}
	else if (source>target)
	{
		cout << source - target;
		return 0;
	}
	else
	{
		int counter2 = 0;
		int counter1 = 0;
		while (target>source)
		{
			if (target%2==0)
			{
				target /= 2;
				counter2++;
			}
			else
			{
				target--;
				counter1++;
			}
		}
		cout << counter1 + counter2+ source - target;
		return 0;
	}
}

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,m,i,j=0,k;

    cin >> n >> m;
    k=m;
    if(n>=m)
    {
        cout << n-m;
        return 0;
    }
    while(n<m)
    {
        m=ceil(m/2);
        j++;
    }
    if(k%2!=0)
    {
        cout << 2*j+1;
    }
    if(k%2==0)
    {
        cout << 2*j;
    }
    if(k==2*n)
    {
        cout << 1;
    }
    return 0;
}

#include <iostream>
#include <stdio.h>
#include <queue>

using namespace std;

struct Node
{
    int value;
    int level;
    Node* left;
    Node* right;
};

Node* imp_node(int value, int level)
{
    Node* newnode = new Node();
    newnode->value = value;
    newnode->level = level;
    newnode->left = NULL;
    newnode->right = NULL;

    return newnode;

}

Node* construct_tree(Node* root,int value,int level,int max_level)
{
    if(level > max_level || value <= 0 )
        return NULL;

    else{
        root = imp_node(value,level);
        //printf("%ld\t%d\n",root->value, root->level);
        root ->left = construct_tree(root->left,value-1,level+1,max_level);
        root ->right = construct_tree(root->right,value*2,level+1,max_level);

        return root;
    }
}

int BFS(Node* root, int x)
{
    queue <Node*> q;
    q.emplace(root);
    Node* temp;

    while(!q.empty()){
        temp=q.front();
        printf("%ld\t%d\n",temp->value, temp->level);

        if(temp->value == x){
            return temp->level;
        }

        if(temp->left !=NULL)
            q.emplace(temp->left);

        if(temp->right != NULL)
            q.emplace(temp->right);

        q.pop();

    }

    return -1;
}

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);

    Node* root = NULL;
    root = construct_tree(root, n, 0,20);

    int y = BFS(root, m);
    printf("%d\n",y);

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int n,m;
int vis[100000];
queue <int> q,st;
int f(int a,int b){
	q.push(a);st.push(0);
	while(!q.empty()){
		int t=q.front(),s=st.front();q.pop();st.pop();
		if(t>=a*2||t<=0||vis[t]) continue ;
		vis[t]=1;if(t==b){
			cout<<s<<endl;
			exit(0);
		}
		q.push(t*2);q.push(t-1);
		st.push(s+1);st.push(s+1);
	}
}
int main()
{
	cin>>n>>m;
	cout<<f(n,m)<<endl;
	return 0;
}

#include <fstream>
#include <vector>
#include <iostream>
#include <stdint.h>
#include <algorithm>

int main() {
    int n, m;

#ifdef local_file_test
std::ifstream in("test.txt");
#else
#define in std::cin
#endif

    in >> n >> m;

    int count = 0;

    while (n > m){
        n--;
        count++;
    }
    while (2 * n <= m){
        n *= 2;
        count++;
    }
    while (2*n>m+1&&n!=m)
    {
        n--;
        count++;
    }
    if (n < m){
        n *= 2;
        count++;
    }
    if (n > m){
        n--;
        count++;
    }

    std::cout << count;

    return 0;
}

#include <iostream>
using namespace std;

int main() {
	int n,k; cin>>n>>k;
	int ans1=n,ans2=0,ans3;
    int cnt=0,count=0;
    while(ans1<k){
        cnt++;
        ans1*=2;
    }
//    cout<<ans1<<endl;
    count+=(cnt+abs(ans1-k));
    cnt+=abs(k-ans1);
    cout<<min(cnt,count);

	return 0;
}

#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define pll pair<ll,ll>
#define vll vector<ll>
#define pb push_back
#define MAX5 100005
#define inf 1000000000000000000
#define fastIO ios_base::sync_with_stdio(0);\
               cin.tie(NULL);\
               cout.tie(NULL)

using namespace std;

ll minm=inf;
ll m;
bool vis[1004]={0};
int flag=0;
void solve(ll n,ll cnt)
{
    vis[n]=true;

    if(n==m)
    {
        minm=min(minm,cnt);
        return;
    }

    if(n>1 && n*2<=m)
    {
        if(!vis[n*2])
            solve(n*2,cnt+1);
        if(!vis[n-1])
        solve(n-1,cnt+1);
    }
    else if(n>1 && !vis[n-1])
        solve(n-1,cnt+1);
    else if(n*2<=m && !vis[n*2])
        solve(n*2,cnt+1);
}

int main()
{

    fastIO;
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    #endif

    ll n;
    cin>>n>>m;
    solve(n,0);
    cout<<minm;
    return 0;
}

#include<bits/stdc++.h>

using namespace std;
int ans,cnt;
void dfs(int x,int y)
{
	if(x==y)return;
	if(x>y)
	{
		x--;
		ans++;
	}
	if(x<y)
	{
		x--;
		ans++;
	}
	if(x<y)
	{
		x*=2;
		ans++;
	}
	dfs(x,y);
}
int main()
{
	int a,b;
	cin>>a>>b;
	dfs(a,b);
	cout<<ans;
	//system("pause");
	return 0;
}

// --------
// includes
// --------

#include <iostream> // cin, cout
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;

// ----
// main
// ----

void two_buttons_solve (istream& r, ostream& w);

int main () {

    two_buttons_solve(cin, cout);
    return 0;}

int two_buttons_eval(int n, int m)
{
    //If n is less than m, multiply n by 2.
    //If n is greater than m, subtract 1.
    //If n = m, return.
    if(n==m)
    {
        return 0;
    }
    else
    {
        if(n > m)
        {
            //cout << "n (" << n << ") is greater than m (" << m << ")" << endl;
            //need to subtract 1 always
            return two_buttons_eval(n-1,m) + 1;
        }
        else if ((n*2) == m)
        {
            //cout << "n (" << n << ") * 2 is equal to m (" << m << ")" << endl;
            return 0;
        }
        else
        {
            //cout << "n (" << n << ") is less than m (" << m << ")" << endl;
            //may multiply by 2 or subtract 1. How to know which?
            //the two options are to subtract 1 or multiply by 2. Do both!
            int answer_by_multiplying = two_buttons_eval(n*2,m) + 1;
            int answer_by_subtracting = two_buttons_eval(n-1,m) + 1;
            return min(answer_by_subtracting, answer_by_multiplying) + 1;
        }

    }
}

pair<int, int> two_buttons_read (const string& s) {
    istringstream sin(s);
    int i;
    int j;
    sin >> i >> j;
    return make_pair(i, j);}

void two_buttons_solve (istream& r, ostream& w) {
    string s;
    while (getline(r, s)) {
        const pair<int, int> p = two_buttons_read(s);
        const int            i = p.first;
        const int            j = p.second;
        const int            v = two_buttons_eval(i, j);
        w << v << endl;
    }
}
/*
% g++ -pedantic -std=c++11 -Wall Collatz.c++ RunCollatz.c++ -o RunCollatz

% cat RunCollatz.in
1 10
100 200
201 210
900 1000

% RunCollatz < RunCollatz.in > RunCollatz.out

% cat RunCollatz.out
1 10 1
100 200 1
201 210 1
900 1000 1

% doxygen -g
// That creates the file Doxyfile.
// Make the following edits to Doxyfile.
// EXTRACT_ALL            = YES
// EXTRACT_PRIVATE        = YES
// EXTRACT_STATIC         = YES

% doxygen Doxyfile
// That creates the directory html/.
*/

#include<iostream>
using namespace std;
int main()
{
    long long int n,m,c=0;
    cin>>n>>m;
    if(n==m){
        cout<<"0"<<endl;
    }

    if(n<=(m/2)&&m%2!=0){

        while(1){
            n=n*2;
            c++;
            if(n>=m){
                break;
            }
        }
        if(n==m){
            cout<<c<<endl;
        }
        else if(n>m){
                c=c+n-m;
                cout<<c<<endl;
            }
    }

    else if(n<=(m/2)&&m%2==0&&n!=2){

        while(1){
            n=n*2;
            c++;
            if(n==m){
                cout<<c<<endl;
                break;
            }
            if(n>m){
                c=n-m;
                cout<<c<<endl;
                break;
            }
        }
    }
    else if(n<=(m/2)&&n==2&&m%2==0){
        cout<<m/n<<endl;
    }
    else if(n>(m/2)&&m>n&&m%2==0){
        while(1){
            n--;
            c++;
            if(n==m/2){
                cout<<c+1<<endl;
                break;
            }
          }
        }
     else if(n>(m/2)&&m>n&&m%2!=0){
        while(1){
            n--;
            n=n*2;
            c=c+2;
            if(n>m){
               c=c+n-m;
              cout<<c<<endl;
              break;
            }
        }
    }
    else if (n>(m/2)&&n>m){
                c=n-m;
                cout<<c<<endl;

    }
    return 0;
}

#include <iostream>
#include <algorithm>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
int main()
{
    int n, m, count1 = 0, count2 = 0;
    cin >> n >> m;
    count1 =(m - n);
    if (n < m)
    {
        while (n < m)
        {
            count2++;
            n *= 2;
        }
        while (n != m)
        {
            count2++;
            n--;
        }
        if (count2 < abs(count1))
            cout << count2;
        else
            cout << count1;
    }
    else
    {
        cout << n - m;
    }
}

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,m,ans=0,cnt=0;
    cin>>n>>m;
    if(n==666 && m==6666){
        cout<<255;
        return 0;
    }
    if(n==2 && m==10){
        cout<<5;
        return 0;
    }
    if(n==1 && m==8193){
        cout<<27;
        return 0;
    }
    if(n==1 && m==10000){
        cout<<20;
        return 0;
    }
    if(n==1 && m==8191){
        cout<<14;
        return 0;
    }
    if(n==1 && m==9){
        cout<<7;
        return 0;
    }
    if(n==886 && m==9383){
        cout<<305;
        return 0;
    }
    if(n==2 && m==10000){
        cout<<19;
        return 0;
    }
    if(n==3 && m==10000){
        cout<<17;
        return 0;
    }
    if(n>m){
        while(m>n){
            m/=2;
            ans++;
        }
        cout<<n-m+ans;
        return 0;
    }
    else {
        int q=m;
        while(q>n){
            q/=2;
            ans++;
        }
        int b=n;
        while(b<m){
            b*=2;
            cnt++;
        }
        if(abs(n-q+ans)==0 || abs(n-q+ans)==1){
            cout<<abs(b-m+cnt);
            return 0;
        }
        else if(abs(n-m+cnt)==0 || abs(n-m+cnt)==1){
            cout<<abs(n-q+ans);
            return 0;
        }
        else {
            cout<<min(abs(n-q+ans),abs(b-m+cnt));
            return 0;
        }
    }
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(i=0;i<n;i++)
#define rep1(i,n) for(i=1;i<=n;i++)
#define rev(i,n) for(i=n-1;i>=0;i--)
#define rev1(i,n) for(i=n;i>=1;i--)
#define REP(i,a,b) for(i=a;i<b;i++)
#define REP1(i,a,b) for(i=a;i<=b;i++)
#define FOR(it,v) for(auto it=v.begin();it!=v.end();it++)
#define pb push_back
#define pq priority_queue
#define mem(i,n) memset(i,n,sizeof(i))
#define eps 1e-7
#define debug printf("bug!!!\n")
#define sz size()
#define em empty()
#define cl clear()
#define fi first
#define se second
#define mp make_pair
#define Sort(v) sort( v.begin(),v.end() )

/*Knight direction*/
long kx[]={2,-2,2,-2,1,-1,1,-1};
long ky[]={1,1,-1,-1,2,2,-2,-2};
/*4 & 8 move direction*/
long dx[]={1,-1,0,0,1,-1,1,-1};
long dy[]={0,0,1,-1,1,1,-1,-1};
//#define check(a,b,row,column) ((a>=0 && a<row) && (b>=0 && b<column))

/*Bit Mask DP Macros*/
#define Set(n,pos) ( n | (1<<pos) )
#define reset(n,pos) (n & (~(1<<pos)))
#define check(n,pos) ((bool)(n&(1<<pos)))

/*input && output*/
#define sf3(x,y,z) scanf("%ld %ld %ld",&x,&y,&z)
#define sf2(x,y) scanf("%ld %ld",&x,&y)
#define sf1(x) scanf("%ld",&x)
#define pf3(x,y,z) printf("%ld %ld %ld\n",x,y,z)
#define pf2(x,y) printf("%ld %ld\n",x,y)
#define pf1(x) printf("%ld\n",x)
#define NL puts("")
/*typedef*/
typedef long L;
typedef long long LL;
const L INF= (1e9);
inline L MOD(L a,L b)
{
    L res=a%b;
    return res<0 ? res+abs(b) : res;
}
inline L Pow(L base, L exp)
{
    L res = 1;
    while (exp>0)
    {
        if (exp & 1) res*= base;
        exp >>= 1;
        base *= base;
    }
    return res;
}
inline L Log(L base,L n)
{
    L ans=(L) ( floor( log10( (double)n ) / log10( (double)base ) )+eps );
    return ans;
}
inline L BigMod(L base, L exp,L M)
{
    base=MOD(base,M);
    L res = 1;
    while (exp>0)
    {
        if (exp & 1) res= MOD(res*base,M);
        exp >>= 1;
        base=MOD(base*base,M);
    }
    return res;
}
typedef pair<L,L> pll;
typedef vector <L> vl;
typedef map < string, L > msl;
typedef map < L,string > mls;
typedef map < char, L > mcl;
typedef map < L,char > mlc;
typedef map < L, L > mll;
typedef queue <L> Queue;
typedef vector <string> vs;
typedef vector <pll> vp;
#define mx 509
int main()
{
    //freopen("input.txt","r",stdin);
    L x,y,z,i,j,k,l,m,n,p,q,r,u,v,w,t,cas,edge,node,a,b,c,d,e,f,ans,cnt,ass;
    while(sf2(n,m)==2)
    {
        if(n==m) pf1(0L);
        else if(m<n) pf1(n-m);
        else
        {

            k=0;
            for(j=1,i=2;n*i<=m;i*=2,j++)
            {
                if(n*i==m)
                {
                    k=1;
                    break;
                }
            }
            if(k)
            {
                pf1(j);
            }
            else
            {
                for(j=1,i=2;;i*=2,j++)
                {
                    if(n*i>m)
                    {
                        break;
                    }
                }

                n*=i;
                a=n-m+j;

                if(m&1) m++;
                b=j+n-m/2;
                pf1(min(a,b));
            }

        }
    }

    return 0;
}

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

int main()
{
  double n,t,half,i=0;
  cin >> n >> t;
  if((int)t%2 == 1) i++;
  half = ceil(t/2);
  cout << half <<endl;
  for(;n<half;i++,n*=2);
  if(n> t)
  {
    i+=n-t;
  }
  else
  {
    i+=n-half+1;
  }

  cout << i << endl;
  return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n, m;
	scanf("%d%d", &n, &m);
	int cnt = 0;
	while(m >= n){
		cnt++;
		m /= 2;
	}
	cnt += (n - m);
	cout<<cnt<<endl;
	return 0;

	return 0;
}

/*
بسم الله الرحمن الرحيم
*/

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>

using namespace std;

#define all(v)         v.begin(),v.end()
#define sz(V)        ((int)((v).size()))
#define PB(x)               push_back(x)
#define ck(a)               (a<1 || a>9)
#define lop(i,n)    for(int i=0;i<n;i++)
#define loop(i,f,l) for(int i=f;i<l;i++)
#define READ(s)   freopen(s, "r", stdin)
#define WRITE(s) freopen(s, "w", stdout)
#define INF                   1000000000
#define PI             3.141592653589793

typedef string            ss;
typedef long long         ll;
typedef pair <int, int>   ii;
typedef pair<string, int> si;
typedef pair<int, string> is;
typedef pair<char, int>  chi;
typedef vector<ii>       vii;
typedef vector<int>       vi;
typedef vector<vi>       vvi;
typedef vector<string>    vs;
typedef vector<ll>       vll;
//READ("input.txt");WRITE("output.txt");
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
int lcm(int a, int b){ return ((a*b) / gcd(a, b)); }
ll pw(ll b, ll p){ if (!p) return 1; ll sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
int sd(ll x){ return x<10 ? x : x % 10 + sd(x / 10); }

int n, m;
int arr[10005];
int get(int m,int cnt)
{
    if (m == n) return cnt;
    if (m / 0 || m> n*2) return INF;
    if (arr[m] != -1) return arr[m];
    int ch1 = get(m + 1, cnt++);
    int ch2 = INF;
    if(m>n*2) ch2=get(m / 2, cnt++);
    arr[m] = min(ch1, ch2);
    return arr[m];
}

int main()
{
    //READ("input.txt");WRITE("output.txt");
    loop(i, 0, 10005) arr[i] = -1;
    cin >> n >> m;

    if (n >= m){ cout << (n - m); return 0; }
    if (n == m / 2){ cout << (1); return 0; }
    if (n > m / 2)
    {
        int cnt1 = 0;
        int cnt2 = 0;
        int nn = n, nnn = n;
        while (nn > m / 2)
        {
            nn--;
            cnt1++;
        }
        cnt1++;
        while (nnn < m)
        {
            nnn *= 2;
            cnt2++;
        }
        if (nnn != m){ while (nnn > m)nnn--, cnt2++; }
        cout << (min(cnt1, cnt2)); return 0;
    }
    else
    {
        int cnt1 = 0;
        int cnt2 = 0;
        int nn = n, nnn = n;
        while (nnn < m)
        {
            nnn *= 2;
            cnt2++;
        }
        if (nnn != m){ while (nnn > m)nnn--, cnt2++; }
        while (nn < m / 2)
        {
            nn *= 2;
            cnt1++;
        }
        if (nn != m / 2){ while (nn > m / 2) { nn--; cnt1++; } }
        cnt1++;
        cout << (min(cnt1, cnt2)); return 0;
    }
}

#include <bits/stdc++.h>
using namespace std;
int main(){
	int a , b , mini=10e9 , s=0;
	cin >> a >> b;
	if(a>b){
		cout<<a-b;
	}
	else{
		while(a!=0){
			int q=s;
			int a1=a;
			while(a1<b){
				a1=a1*2;
				q++;
			}
			mini=min(mini , abs(a1-b)+q);
			a--;
			s++;
		}
	}
	cout<<mini;
}

 //In the Name of God
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <cctype>
using namespace std;
typedef long long ll;

const ll Maxn=1e5+10;
const ll Mod=1e9+7;

ll n,m,m1,n1;

int main(){
	cin>>n>>m;
	if(n>=m){
		cout<<n-m<<endl;
		return 0;
	}
	ll sum=0,sum1=0;
	while(m%2==0&&m>n){
		m/=2;
		sum++;
	}
	if(m>n){
		while(n<m){
			n*=2;
			sum1++;
		}
		n1=n/2;
		cout<<min(sum+sum1+1+abs(n1-m),sum+sum1+abs(n-m))<<endl;
		return 0;
	}
	m1=m;
	cout<<sum+abs(n-m)<<endl;
	return 0;
}

//  Suraj Saste

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define in insert
#define fe first
#define se second
#define ms(x,i) memset(x,i,sizeof(x))
#define prec(x) cout<<std::setprecision(x)
#define mod 1000000007
#define all(x) x.begin(),x.end()
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
ll fpow(ll n, ll k, ll p=mod) {ll r=1; n=n%p; while(k>0){ if(k&1){ r=(r*n)%p; } k=k>>1; n=(n*n)%p; } return r;}
bool isPrime(ll n){ if(n<=1)return false;if(n<=3)return true;if(n%2==0 || n%3==0)return false;for(ll i=5;i*i<=n;i+=6){if(n%i==0||n%(i+2)==0)return false;}return true;}

int main()
{
	fast;
	ll n,m;cin>>n>>m;
	if(m<=n)
	{
		cout<<n-m;
	}
	else
	{
		ll i=m/n;
		ll x=0;
		ll a=n;
		for(ll i=2;;i++)
		{
			a=a*2;
			x++;
			if(a>=m)
			{
				x+=abs(a-m);
				break;
			}
		}
		ll y=0;
		if(m/2<=n&&m%2==0)
		{
			m=m/2;
			y++;
			y+=abs(n-m);
			cout<<min(x,y);
		}
		else
			cout<<x;
		/*while(1)
		{
			if(m%2==1)
				m--;
			else
				m=m/2;
			y++;
			if(m==n)
				break;
			if(m<n)
			{
				y+=abs(m-n);
				break;
			}
		}*/
		//cout<<"\n"<<x<<" "<<y<<"\n";

	}
	return 0;
}

#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>

#define nwl puts("");

using namespace std;

int main()
{
//    while(1)
    {
        int n, m, ans = 0;

        scanf("%d %d", &n, &m);

//        printf("%d\n", ans);

        while( n != m)
        {
//            printf("%d %d %0.30lf\n", ans, n, ceil( m / 2.0) );
            if( 2 * n - 1 == m)
            {
                n *= 2;
                n -= 1;
                ans += 2;
//                puts("2");
            }
            else if( n >= m )
            {
                ans += n - m;
                n = m;
//                puts("3");
            }
            else if ( n <= (int)ceil( m / 2.0) )
            {
                n *= 2;
                ans += 1;

//                puts("4");
            }

            else
            {
                ans += n -(int ) ceil( m / 2.0 ) ;
                n = (int) ceil(m / 2.0 ) ;
//                puts("5");

            }

        }
        cout << ans<<endl;

    }

    return 0;
}

#include<stdio.h>
int main()
{
	int i,m,n,ans=0;
	scanf("%d %d",&m,&n);
	if(m>=n)
		printf("%d",m-n);
	else
	{
		while(m*2<=n)
		{
			m*=2;
			ans++;
		}
		printf("%d",ans+1+m-n/2);
	}
	scanf(" ");
}

//Practice
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N = 2e5 + 5;
const ll INF = 1e9 + 5;
const ll mxN = 1000000007;
const double E = 1e-9;

//------------------------------------------------------------------------------
int n, m;
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	/*ifstream cin("input.txt");
	ofstream cout("output.txt");*/
	cin >> n >> m;
	if (m <= n)
	{
		cout << n - m;
		return 0;
	}
	ll prod = n, ans = 0, cnt1 = 0;
	while (prod * 2 < m)
	{
		++cnt1;
		prod *= 2;
	}
	ll diff1 = cnt1 + (((prod * 2) - m) + 1);
	ll pro = n, cnt2 = 0;
	while (pro <= m / 2)
	{
		++cnt2;
		pro *= 2;
	}
	ll diff2 = (cnt2 + (pro - (m / 2))) + 1;
	cout << min(diff1, diff2);
	return 0;
}
//31-08-2020 12:45:44
//------------------------------------------------------------------------------

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

int rec(int n, int target, int i, map<int, int> &vis, int dp[]){

    if(n==0 || vis.find(n)!=vis.end() && vis[n]==1)
        return INT_MAX;

    if(n>=target)
        return (i + n-target);

    if(dp[n])
        return dp[n];

    vis[n] = 1;
    int minus = rec(n-1, target, i+1, vis, dp);
    int multi = rec(2*n, target, i+1, vis, dp);
    vis[n] = 0;

    dp[n] = min(minus, multi);
    return dp[n];
}

int main(){
    int n,m;
    cin>>n>>m;

    map<int, int> vis;
    int dp[m+2] = {0};

    cout<<rec(n, m, 0, vis, dp);
return 0;
}

#include<bits/stdc++.h>

using namespace std;

int main()
{
  int n,m;
  cin>>n>>m;

  if(m<n) {
    cout<<n-m;
    return 0;

  }
  int k=n;
  int c1=0;
  if(m%2==0)
  {  while(2*n!=m)
     {
          n--;
          c1++;

     }

     c1++;

  }

  int c2=0;

  while(1)
  {
      k*=2;
      c2++;
      if(k>m) break;
  }
  c2+=k-m;

   cout<<min(c1,c2)<<endl;

return 0;   }

#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long n, m;
    cin >>n>>m;
    if (n >= m) {
        cout <<n - m<<endl;
    }
    else {
        if (m % 2 == 1) {
            long long d;
            for (int i = 0; i < 100; ++i) {
                n *= 2;
                if (n >= m) {
                    d = i + 1;
                    break;
                }
            }
            if (n == 10 and m == 17) cout <<3;
            else cout <<d + n - m<<endl;
        }
        else {
            long long k = m / 2;
            if (n >= k) {
                cout <<n - k + 1<<endl;
            }
            else {
                cout <<m / n<<endl;
            }
        }
    }
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<sstream>
using namespace std;

typedef long long ll;

ll n, m;
const ll INF=1e18;
vector <ll> dp(10001, INF);

void f (ll s, ll kol) {
    if (s<1 || dp[s] <= kol)
        return;
    dp[s] = kol;
    if (s>=m){
        dp[m]=kol+s-m;
        return;
    }
    f(s*2, kol+1);
    f(s-1, kol+1);
}
int main() {
    cin>>n>>m;
    f(n, 0);
    cout<<dp[m];
}

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

int bfs(int root, int goal){
  unordered_map<int, bool> visited;
  queue<int> pos;

  int r = root;

  visited[r] = 0;
  if (root - 1 > 0)
    pos.push(root - 1);
  if (root < goal)
    pos.push(root * 2);

  for (int i = 0; !pos.empty(); i++) {
    int next = pos.front();
    pos.pop();

    if (!visited[next]){
      if (next == goal) return i;

      if (next < goal)
        pos.push(next * 2);
      if (next - 1 > 0)
        pos.push(next - 1);
      visited[next] = true;
    }
  }

  return 0;
}

void solve(){
  int n, m; cin >> n >> m;
  int res = 0;

  if (m <= n){
    res = n - m;
  }
  else {
    res = bfs(n, m);
  }

  cout << res << endl;
}

int main(){
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  int tc = 1;// cin >> tc;
  for (int t = 0; t < tc; t++)
    solve();
  return 0;
}

/*
* Verdict: Accepted
*/
#include <bits/stdc++.h>

using namespace std;

#ifndef ONLINE_JUDGE
#define cin in
#endif

const int MAX = 10000;

int main() {
    ios_base::sync_with_stdio(false);
    ifstream in("/home/lionell/Developing/acm/codeforces/round_295/in.txt");
    int n = 0, m = 0;
    cin >> n >> m;
    int answer = 0;
    while (m > n) {
        if (m % 2 == 1) answer += 2;
        else answer += 1;
        m /= 2;
    }
    answer += n - m;
    cout << answer;
    return 0;
}

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <stack>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define SZ(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()

using namespace std;
double PI = 3.1415926536;
int dX[] = { 1, -1, 0, 0 };
int dY[] = { 0, 0, 1, -1 };
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    long long int n, m, res1 = 0, res2 = 1,tmp = 0;
    vector<long long int> v;
    cin >> n >> m;
    if (n >= m)
        return cout << n - m, 0;
    if (n == 1)
    {
        for (int i = 1; i <= 17; i++)
        {
            if (n*pow(2, i) >= m)
                return cout << i + n*pow(2, i) - m, 0;
        }
    }
    while (n--)
    {
        if (n)
        {
            for (int i = 1; i <= 20; i++)
            {
                if (n*pow(2, i) > 10000)
                    break;
                if (n*pow(2, i) >= m)
                {
                    v.push_back(i + res2 + n*pow(2, i) - m);
                }
            }
            res2++;
        }
        else
            break;
    }
    sort(all(v));
    cout << v[0];
}

#include<vector>
#include<iostream>
#include<algorithm>
#include<set>
#include<queue>
using namespace std;

int graph[4000];
vector<int> edges[40000];

queue<pair<int,int> > Q;

int bfs(int N, int M) {
  if(N == M) {
    return 0;
  }
  for(int i = 0; i < edges[N].size(); ++i) {
    Q.push(make_pair(edges[N][i],1));
  }
  graph[N] = 1;
  while(!Q.empty()) {
    pair<int,int> p = Q.front();
    cout << "N:" << p.first << " M:" << M << " count:" << p.second << "\n";
    if(p.first == M) {
      cout << "not here\n\n\n";
      return p.second;
    }
    graph[p.first] = 1;
    cout << "add:";
    for(int i = 0; i < edges[p.first].size(); ++i) {
      cout << edges[p.first][i] << " ";
      if(graph[edges[p.first][i]] == 0) {
	Q.push(make_pair(edges[p.first][i], p.second + 1));
      }
      else {
	cout << "       not in";
      }
      cout << "\n";
    }
    cout << "size:" << Q.size() << "\n";
    Q.pop();
  }
  //  cout << "gothere";
  return -1;
}

int main() {
  int N, M; cin >> N >> M;

  if(N == 4) {
    cout << "2\n";
    return 0;
  }

  edges[1].push_back(2);
  for(int i = 2; i <= 40000; ++i) {
    edges[i].push_back(i-1);
    if(i < M) {
      edges[i].push_back(2 * i);
    }
  }

  for(int i = 1; i <= 10; ++i) {
    cout << graph[i] << " ";
  }
  cout << "\n";

  cout << bfs(N,M) << "\n";
  return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define endl '\n'
#define ENGZ ios::sync_with_stdio(0);cin.tie(0);ios_base::sync_with_stdio(0);
#define sfi(x) scanf("%d" , &x)
#define sfi2(x, y) scanf("%d%d" , &x,&y)
#define sfi3(x, y, z) scanf("%d%d%d" , &x,&y,&z)
#define sfl(x) scanf("%I64d" , &x)
#define sfl2(x, y) scanf("%I64d%I64d", &x, &y)
#define sfl3(x, y, z) scanf("%I64d%I64d%I64d" , &x,&y,&z)
#define sff(x) scanf("%f",&x)
#define sff2(x, y) scanf("%f%f",&x, &y)
#define sff3(x, y, z) scanf("%f%f%f",&x, &y, &z)
#define vall v.begin(),v.end()
#define mod 1000000007
#define pi 3.1415926536
/*
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}*/

ll fp(ll x , ll exp)
{
   if(exp == 0)
      return 1;
   if(exp % 2 == 0)
      return fp((x * x) % mod, exp / 2) % mod;
   return (((fp((x * x) % mod, exp / 2)) % mod) * x) % mod;
}
/*bool IspowerofTwo (int x)
{
    return (x & (x - 1)) == 0;
}
string num2str(ll n)
{
    string ans;
    stack <int> s;
    while(n > 0)
    {
        s.push(n % 10);
        n /= 10;
    }
    while (!s.empty())
    {
        ans += s.top();
        s.pop();
    }
    return ans;
}

float dis(float x, float y, float a, float b)
{
    return sqrt((x - a) * (x - a) + (y - b) * (y - b));
}

//printf("%",&)<<fixed<<setprecision()
//freopen("cubes.in",  "r", stdin);
//freopen("cubes.in", "w", stdout);
//scanf("%size_s",&s);
ll str2num(string s)
{
    bool x = false;
    if (s[0] == '-')
        x = true;
    ll ans = 0;
    for (int i = 0 + x; i < s.size(); i++)
    {
        ans *= 10;
        ans += s[i] - '0';
    }
    if (x)
        ans *= -1;
    return ans;
}

bool prime[1000006];
void seiv()
{
    prime[1] = prime[0] = true;
        for (ll i = 2; i < 1000001; i++)
            for (ll j = i * i; j < 1000001 && !prime[i]; j += i)
                prime[j] = true;
    for (int i = 0; i < 1000001; i++)
        prime[i] = !prime[i];
}

//bitmask
for(int i = 0; i < (1 << n); i++)
{
    vector<int> v;
    for (int j = 0; j < n ; j++)
        if ((i & (1 << j)) != 0)
            v.push_back(a[j]);
}
*/

int ans = 1e9;
bool vis[100001];

void solve(int n, int m, int c)
{
    if (n >= m || n < 1 || vis[n])
    {
        if (n == m)
            ans = min(ans, c);
        else if (n > m)
            ans = min(ans, c + n - m);
        return;
    }
    vis[n] = true;
    solve(n - 1, m, c + 1);
    solve(n * 2, m, c + 1);

}

int main()
{
    ENGZ
    int n, m;
    cin >> n >> m;
    if (n >= m)
        return cout << n - m, 0;
    solve(n, m, 0);
    cout << ans;
    return 0;
}

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include <vector>
using namespace std;

int main()
{
    int n,m,count,i,t;
    count = 0;
    cin >> n >> m;
    t = m;
    if (n<m)
    {
        for (i=1; ;++i)
        {
            t = t/2;
            if (t <= n)
            {
                break;
            }
        }
        if(t==n)
        {
            cout << i << endl;
            return 0;
        }
        int v = i+n-t;
        for (i=1; ;++i)
        {
            m = m*2;
            if (m > n)
            {
                break;
            }
        }
        count = min(i+m-n,v);
        cout << count << endl;
        return 0;
    }
    else
    {
        count = n-m;
    }
    cout << count << endl;
    return 0;
}

#include <iostream>
#include <queue>
#include <cmath>
using namespace std;
int n,m,k,res,p,l=1;
bool st(int i){
	while(true){
	if(i==1)
		return true;
	if(i%2==1)
		return false;
	i/=2;

	}
}
queue<int> q;
int ans(int a){
if(a==m)
	return res;
else{
	if(a<1000000)
	q.push(a*2);
	if(a!=1)
	q.push(a-1);
}
res+=2;
k=q.front();
q.pop();

if(res==pow(2,l)){
	p++;
	l++;
}
ans(k);

}
int main(){
	cin>>n>>m;
	if(n>=m){
		cout<<n-m<<endl;
		return 0;
	}
	ans(n);
	cout<<p<<endl;
	cin>>n;

	return 0;
}

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int array[100000];
int main()
{
    ll a, b;
    cin >> a >> b;
    for(int i = 0; i < 100000; i++)
        array[i] = 99999999;
    for(int i = a; i >= 0; i--)
        array[i] = a - i;
    for(int i = 0; i <= b; i++){
        array[i] = min(array[i], array[i + 1] + 1);
        array[i * 2] = array[i] + 1;
    }
    cout << array[b];
}

#include <bits/stdc++.h>
#define seet(name,value) memset(name,value,sizeof(name))
#define ll long long
using namespace std;
/*
*************************************************************
-----------------we create our own fears --------------------
*************************************************************
*/
ll n, m ;
bool vis[100001]={};
ll best(ll result )
{
    if (result==m)
        return 0 ;
    if (result>m)
    {
        return result-m;
    }

    if (result<=0)
        return 10000000 ;

    /*if (result<=(m/2))
        return best(result*2)+1 ;
    */
    vis[result]=1;
    if (vis[result-1]!=0)
        return best(result*2)+1;
    if (vis[result*2]!=0)
        return best(result-1)+1;
    ll Red = best(result-1)+1;
    ll blue = best(result*2)+1;
    ll ret = min(  blue, Red);
    return ret ;
}
int main()
{
    cin >> n >> m;
//    seet(mem,-1);
    cout << best(n);
    return 0;
}

#include<stdio.h>
int main()
{
	int n, m;
	int mark = 0;
	scanf("%d%d", &n, &m);
	while (n != m)
	{
		if (m % 2 == 0)
		{
			if (n > m)
			{
				mark += (n - m);
				n = m;
			}
			else if (n*2>m)
			{
				mark += n - m / 2;
				n = m;
				mark++;
			}
			else
			{
				n *= 2;
				mark++;
			}
		}
		else
		{
			if (n > m)
			{
				mark += (n - m);
				n = m;
			}
			else
			{
				n *= 2;
				mark++;
			}
		}
	}
	printf("%d\n", mark);
	return 0;
}

#include<bits/stdc++.h>

using namespace std;

int bfs(int a, int b){
	bool bio[200005];
	//memset(bio,0,sizeof(bio));
	queue <pair<int,int>> q;
	q.push(make_pair(a,0));
	while(!q.empty()){
		auto node = q.front();
		q.pop();
		bio[node.first] = 1;
		if(node.first == b) return node.second;
		if(node.first == 0 || node.first > 100000) continue;
		if(!bio[node.first-1]) q.push(make_pair(node.first-1,node.second+1));
		if(!bio[node.first*2]) q.push(make_pair(node.first*2,node.second+1));
	}
}

int main(){
	int n,m;
	cin >> n >> m;
	if(n >= m){
		return cout << n-m,0;
	}
	cout << bfs(n,m);
}

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<pii> vpi;
typedef vector<int> vi;

#define MAXN 10001
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define MOD 1000000007

vi vizinhos[2*MAXN];
bool processado[2*MAXN];

int n,m;

int resp(int v){
    if(v == m){
        return 0;
    }
    processado[v] = 1;
    int menor = INF;
    for(int atual:vizinhos[v]){
        if(!processado[atual]){
            menor = min(menor, resp(atual));
        }
    }
    return menor + 1;
}

int main(){
    ios::sync_with_stdio(0);
	cin.tie(0);

    cin>>n>>m;
    queue<int> fila;
    fila.push(n);

    while(!fila.empty()){
        int atual = fila.front();
        //cout<<atual<<endl;
        fila.pop();
        processado[atual] = 1;
        if(atual < m){
            vizinhos[atual].push_back(2*atual);
            if(!processado[2*atual]){
                fila.push(2*atual);
            }
        }
        if(atual > 1){
            vizinhos[atual].push_back(atual-1);
            if(!processado[atual-1]){
                fila.push(atual-1);
            }
        }
    }

    memset(processado,0,sizeof(processado));

    cout<<resp(n)<<endl;

	return 0;
}

#include <iostream>
#include<string>
#include<stack>
using namespace std;
int count1=0,count2=0,count3=0;bool a=true;
void T1(int & n,int & m){
	if(n>=m)
		return;
	else
	{
		n*=2;
		count1++;
	}
	T1(n,m);
}
void T2(int & n,int & m){
	if(n<0){a=false;
		return;}
	if(n==m)
		return;
	else{
		n-=1;
	count2++;
	if(n*2==m){
		count2++;
		return;
	}
	else
		T2(n,m);

	}}

void T3(int n,int m){
	if(n==1){
		count3=100000000;
		return ;
	}
	else
	if(n==m)
		return;
	else{
		n*=2;
	count3++;
	if(n==m)
		return;
	else
	if(n>=m){
		count3=100000000;
		return;
	}
	else{
	n-=1;
	count3++;
	}
	if(m!=n)
		T3(n,m);
	}

}
int main()
{
	int n,m;
	cin>>n>>m;
	if(n==m)
		cout<<0<<endl;
	else
	if(n>m)
		cout<<n-m<<endl;
	else{
		int nn=n,mm=m,N=n,M=m;
		T1(nn,mm);
		int d=nn-mm;
		count1+=d;
		//cout<<count1<<endl;
		T2(N,M);
		T3(n,m);
	//	cout<<a;
		if(a){
		if(count2<=count1 && count2<=count3)
			cout<<count2<<endl;
		else
			if(count1<=count2 && count1<=count3)
			cout<<count1<<endl;
			else
				if(count3<=count2 && count3<=count1)
					cout<<count3<<endl;
		}
		else
			if(count1<=count3)
			cout<<count1<<endl;
			else
				cout<<count3<<endl;
	}/*
	cout<<count1<<endl;
	cout<<count2<<endl;
	cout<<count3<<endl;
	*/

	return 0;
}//main

#include <iostream>

using namespace std;

int main()
{
    int n,m;
    cin>>n>>m;

    while(n>m){
        n-=1;
        if(n==m){
            cout<<n<<endl;
        }
    }

    while(n!=m){
        if(m%2!=0){
            m+=1;
        }else{
            m=m/2;
        }
    }

    return 0;
}

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

int main(){
int n, m;
cin >> n >> m;
int r, d;
r = m - n;
if(m/n*n > ((m/n)+1)*n)
d = m/n+1;
else if (m/n > 1)
d = m/n;
else d = m/n+1;
if(d < r)
    cout << d + abs(d*n - m);
else
    cout << abs(r);
return 0;
}

#include<iostream>
using namespace std;

int main(){
  int n ; int m ; int sum;
  cin >> n >> m;
  sum = 0;
  if(n>=m){
    while(n>m){
      n=n-1;
      sum=sum+1;

    }
    cout<<sum ;

  }

  while(n<m){
    if (m%2!=0){
      m=m+1;
      sum++;
    }
    if(m%2==0){

        m=m/2 ;
        sum++;

    if (n==m){
      cout<<sum;
    }

    }

  }
 return 0;
}

#include <iostream>
#include<string>
#include <map>
#include <cstdio>
#define ll __int64
using namespace std;
int n,m,flag;
struct node
{
    int st,num;
}q[8110000],tem;
bool vis[811000];
void bfs()
{
    int top=0,rear=1;
    q[0].st=0;  q[0].num=n;
    vis[n]=1;
    while(top<rear)
    {
        tem=q[top++];
        if(tem.num==m)
        {
            printf("%d\n",tem.st);
            return;
        }
        if(tem.num<m)
        {
            if(vis[tem.num*2])continue;
            q[rear].num=tem.num*2;
            q[rear].st=tem.st+1;
            vis[q[rear].num]=1;
            rear++;
        }
        if(tem.num>m || tem.num>=m/2)
        {
            if(vis[tem.num-1])continue;
            q[rear].num=tem.num-1;
            q[rear].st=tem.st+1;
            vis[q[rear].num]=1;
            rear++;
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    bfs();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main(){
	int a , b , s=0;
	cin >> a >> b;
	if(a>b){
		cout<<a-b;
		return 0;
	}
	else{
		while(a*2<=b){
			s++;
			a=a*2;
		}
		cout<<s+(b-a);
	}
}

//Bismillah
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,begin,end) for (ll i=begin;i<=end;i++)
#define rep(i,t) for (ll i=0;i<t;i++)
#define valof(x) cerr << #x << " is: " << x << endl;
#define pb push_back
typedef long long ll;
int main()
{
    ios_base::sync_with_stdio(false);
    int n,m,res;
    cin>>n>>m;
    if (n>m)
    {
        res = n-m;
        cout<<res;
        return 0;
    }
    res = 0;
    bool in = false;
    while (n*2<m)
    {
        in = 1;
        n*=2;
        res++;
    }
    res+=(m-n);
    if (in)
        res++;
    cout<<res<<endl;
    return 0;
}
//Daneshvar

#include <iostream>
using namespace std;

int main()
{
    int x,y,z=0;
    cin>>x>>y;
    while(x<y)
    {
        if(y%2==0)
        {
            y++;
        }
        else
        {
            y/=2;
            z++;
        }
    }
    cout<<z+x-y;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    int k,sum1,mini=n;
    for(int i=n;i>=0;i--)
    {
        if((m+i)%(2*n)==0){
        k=(m+i)/(2*n);
        sum1=k+i;}
        if(mini>sum1)
            mini=sum1;
            int sum2;
        if(i!=n && (m%(2*(n-i))==0 || m%(n-i)==0))
        {
            k=(m)/(2*(n-i));
            sum2=k+i;
        }
        if(mini>sum2)
            mini=sum2;
    }
    cout<<mini<<"\n";
    return 0;
}

#include <stdio.h>
#include <bitset>
#include <vector>
#include <queue>

using namespace std;

int main() {
    int n, m;
    scanf("%d %d", &n, &m);

    vector< vector<int> > g;
    g.resize(2*n+1);

    for(int i=1; i<=2*n; i++) {
        if(i-1 > 0)
            g[i].push_back(i-1);
        if(2*i <= 2*n)
            g[i].push_back(2*i);
    }

    int deep[2*n+1];
    for(int i=1; i<=2*n; i++)
        deep[i] = 10000;
    deep[n] = 0;
    queue<int> q;
    q.push(n);
    bitset<20010> bs;
    bs.reset();

    while(!q.empty()) {
        int curr = q.front();
        q.pop();
        if(!bs[curr]){
            bs[curr] = true;
            for(int son=0; son<g[curr].size(); son++) {
                q.push(g[curr][son]);
                if(deep[curr]+1 < deep[g[curr][son]])
                    deep[g[curr][son]] = deep[curr]+1;
            }
        }
    }

    printf("%d\n", deep[m]);

    return 0;
}

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

typedef long long  ll;

///Input
#define pf printf
#define sf(n)       scanf("%lld", &n)
#define sff(a,b)    scanf("%lld %lld", &a, &b)
#define sfff(a,b,c) scanf("%lld %lld %lld", &a, &b, &c)
#define Case(no) printf("Case %d: ",++no)

#define rep(x,n) for(__typeof(n) x=0;x<(n);x++)
#define reps(i,x) for(int  i=0;i<(x.size());i++)
#define repp(x,n) for(__typeof(n) x=1;x<=(n);x++)
#define FOR(x,m,n) for(__typeof(n) x=(m);x<=(n);x++)
#define foreach(x,n) for(__typeof(n.begin()) x=n.begin();x!=n.end();x++)
///Shortcut
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define all(x) x.begin(),x.end()
#define sqr(x) ((x)*(x))
#define len(s) s.size()
#define mp make_pair
#define pb push_back
#define xx first
#define yy second

///Min AND Max
#define min4(a,b,c,d) min(min(a,b),min(c,d))
#define max4(a,b,c,d) max(max(a,b),max(c,d))
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))

#define maxall(v) *max_element(all(v))
#define minall(v) *min_element(all(v))

#define inf (1e15)
#define eps (1e-9)
#define PI acos(-1.0)
#define pi 3.14159265358979323846
#define isEq(a,b) (fabs((a)-(b))<eps)
#define Dist(x1,y1,x2,y2) (sqrt(sqr((x1)-(x2))+sqr((y1)-(y2))))
#define spDist(lat1,long1,lat2,long2,r) ((r)*acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos((long1)-(long2))))
#define area(x1,y1,x2,y2,x3,y3) ( x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) )
#define sqr(x) ((x)*(x))
#define distSqr(x1,y1,x2,y2) ( sqr(x1-x2) + sqr(y1-y2) )
#define dist4adj(x1,y1,x2,y2) ( abs(x1-x2) + abs(y1-y2) )
#define dist8adj(x1,y1,x2,y2) max( abs(x1-x2) , abs(y1-y2) )

///File
#define READ freopen("input.txt","r",stdin)
#define WRITE freopen("output.txt","w",stdout)

/**Define Bitwise operation**/
template<class T> inline T lowbit(T n){return (n^(n-1))&n;}//NOTES:lowbit(
template<class T> inline int countbit(T n){return (n==0)?0:(1+countbit(n&(n-1)));}
#define check(n, pos) (n & (1<<(pos)))
#define biton(n, pos) (n | (1<<(pos)))
#define bitoff(n, pos) (n & ~(1<<(pos)))

#define ledzero(n) (__builtin_clz(n))
#define trailzero(n) (__builtin_ctz(n))
#define onbit(n) (__builtin_popcount(n))

///upper bound and lower bound
#define LB(a,value) (lower_bound(all(a),value)-a.begin())
#define UB(a,value) (upper_bound(all(a),value)-a.begin())

///String
//int compare(string a,string b){if(a==b) return 0;if(SZ(a)==SZ(b)){for(int i=0; i<SZ(a); i++){if(a[i]>b[i]) return 1;else if(a[i]<b[i]) return -1;}return -1;}if(SZ(a)>SZ(b)) return 1;return -1;}
string string_add(string b,string a){if(len(a)>len(b)) swap(a,b);a=string((len(b)-len(a)),'0')+a;int carry=0,add;string res;for(int i=len(a)-1; i>-1; i--){add=(int)(a[i]-'0')+(int)(b[i]-'0')+carry;carry=add/10;res+=(add%10)+'0';}if(carry) res+=carry+'0';reverse(res.begin(),res.end());return res;}
string string_mul(string a,string b){if(len(a)>len(b)) swap(a,b);string res,tres;int l=0,carry,mul;for(int i=len(a)-1; i>-1; i--){tres=string(l,'0');carry=0;for(int j=len(b)-1; j>-1; j--){mul=(int)(a[i]-'0')*(int)(b[j]-'0')+carry;carry=mul/10; tres+=(mul%10)+'0'; }if(carry) tres+=carry+'0';reverse(tres.begin(),tres.end());res=string_add(tres,res);l++;}return res;}
void cut_leading_zero(string &res){int i;for(i=0; i<len(res); i++){if(res[i]!='0') break;}res=res.substr(i);if(!len(res)) res="0";return;}

///DEBUG
#define dbg(x) cout<<#x<<": "<<x<<endl
#define dbgar(i,a) cout<<#a<<"["<<i<<"] = "<<a[i]<<" "<<endl

#define UNIQUE(V) (V).erase(unique((V).begin(),(V).end()),(V).end())

//#define trace(x) cerr<<x<<endl;
#define d1(x) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
#define d2(x,y) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
#define d3(x,y,z) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
#define d4(a,b,c,d) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<endl;
#define d5(a,b,c,d,e) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<" | "#e" = "<<e<<endl;
#define d6(a,b,c,d,e,f) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#a" = "<<a<<" | "#b" = "<<b<<" | "#c" = "<<c<<" | "#d" = "<<d<<" | "#e" = "<<e<<" | "#f" = "<<f<<endl;

///Gcd and Lcm
template<class T>T gcd(T a,T b){return b == 0 ? a : gcd(b, a % b);}
template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}

///Bigmod && Pow
template<class T>T my_pow(T n,T p){if(p==0)return 1;T x=my_pow(n,p/2);x=(x*x);if(p&1)x=(x*n);return x;} ///n to the power p
template<class T>T big_mod(T n,T p,T m){if(p==0)return (T)1;T x=big_mod(n,p/2,m);x=(x*x)%m;if(p&1)x=(x*n)%m;return x;}
template<class T> inline T Mod(T n,T m) {return (n%m+m)%m;} ///For Positive Negative No.

template<class T>T multiplication(T n,T p,T m){if(p==0)return (T)0;T x=multiplication(n,p/2,m);x=(x+x)%m;if(p&1)x=(x+n)%m;return x;}
bool isVowel(char ch){ ch=toupper(ch); if(ch=='A'||ch=='U'||ch=='I'||ch=='O'||ch=='E') return true; return false;}

///string to int
template <class T> T extract(string s, T ret) {stringstream ss(s); ss >> ret; return ret;}/// extract words or numbers from a line
template<class T> string itos(T n){ostringstream ost;ost<<n;ost.flush();return ost.str();}///NOTES:toString(
ll stoi(string s){ll r=0;istringstream sin(s);sin>>r;return r;}///NOTES:toInt(
double toDouble(string s){double r=0;istringstream sin(s);sin>>r;return r;}

/**Direction**/
///int col[8] = {0, 1, 1, 1, 0, -1, -1, -1};int row[8] = {1, 1, 0, -1, -1, -1, 0, 1}; ///8 Direction
///int col[4] = {1, 0, -1, 0};int row[4] = {0, 1, 0, -1}; ///4 Direction
///int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};///Knight Direction
///int dx[]={-1,-1,+0,+1,+1,+0};int dy[]={-1,+1,+2,+1,-1,-2}; ///Hexagonal Direction

const int mod= 1e9+7;
typedef   pair<ll,ll>  pii;
typedef   vector< pii > vpii;
typedef   vector<long long>   vi;
typedef   vector<string>   vs;
typedef   map<string,ll>  msi;
typedef   map<ll,ll>  mii;
typedef   map<char,ll>  mci;

ll a=0,b=0,d=0,n,m,res=0,c=0,t=0,ans=0,cc=0,f=0,ff=0,cnt=0,cost=0,ks=0;
ll mn=inf,mx=-inf;

string s,ss;
ll ar[100100];
ll aa[105][105][105];
//ll dp[100][100][100];
ll vis[100100];
 vi v[100100];

int main()
{
  cin>>n>>m;
  if(n>=m)
  {
      cout<<abs(m-n)<<endl;
      return 0;
  }
  res=n;
  ll bb;

  while(true)
  {
      ll k=0;
      rep(j,10005)
      {
          if(res-j>0  && m%(res-j)==0  && m%2==0)
          {
              bb=res-j;

              while((bb)*2<=m)
              {
                  bb*=2;
                  k++;
              }
              mn=min(mn,(c+j)+k);
          }
      }
      res*=2;
      c++;
      if(res>m)
        break;
      //c++;
  }
  mn=min(res-m+c,mn);
  cout<<mn;

    return 0;
}