/*
*
*
*想用结构体做下,结果一直RE,1 40000,不会有结果。用数组做了就AC了....待解决
#include<iostream>
#include<cstdlib>
#include<queue>
#include<string.h>
#include<cstdio>
using namespace std;
const int Max = 100000+5;
int N, K;
bool pos[Max];
//int dir[3] = {-1, 1};
struct Node
{
int p; //position
int step;
Node(){
p = 0;
step = 0;
}
Node(int q, int b){
p = q, step = b;
}
};
Node tmp1;
void bfs()
{
queue<Node> q;
while(!q.empty()){
q.pop();
}
Node tmp;
tmp.p = N;
tmp.step = 0;
// cout << tmp.p << endl;
q.push(tmp);
pos[tmp.p] = true;
while(!q.empty()){
tmp1 = q.front();
if(tmp1.p == K){
return;
}
q.pop();
for(int i=0; i<3; i++){
Node tmp2;
if(i == 1){
tmp2.p = tmp1.p*2;
}
else
tmp2.p = tmp1.p+i-1;
cout << tmp2.p << endl;
//judge
if((!pos[tmp2.p]) && tmp2.p >=0 &&tmp2.p <= Max){
tmp2.step = tmp1.step+1;
// cout << tmp2.t << " ";
q.push(tmp2);
pos[tmp2.p] = true;
}
}
// cout << endl;
}
}
int main()
{
while(cin >> N >> K){
memset(pos, false, sizeof(pos));
bfs();
cout << tmp1.step << endl;
}
return 0;
}*/
#include<iostream>
#include<cstdlib>
#include<queue>
#include<string.h>
#include<cstdio>
using namespace std;
const int Max = 100000+5;
int N, K;
bool pos[Max];
int step[Max];
//int dir[3] = {-1, 1};
struct Node
{
int p; //position
int step;
Node(){
p = 0;
step = 0;
}
Node(int q, int b){
p = q, step = b;
}
};
int tmp1;
void bfs()
{
queue<int> q;
while(!q.empty()){
q.pop();
}
int tmp;
tmp = N;
step[tmp] = 0;
// cout << tmp.p << endl;
q.push(tmp);
pos[tmp] = true;
while(!q.empty()){
tmp1 = q.front();
if(tmp1 == K){
return;
}
q.pop();
for(int i=0; i<3; i++){
int tmp2;
if(i == 1){
tmp2 = tmp1*2;
}
else
tmp2 = tmp1+i-1;
// cout << tmp2 << endl;
//judge
if((!pos[tmp2]) && tmp2 >=0 &&tmp2 <= Max){
step[tmp2] = step[tmp1]+1;
// cout << tmp2.t << " ";
q.push(tmp2);
pos[tmp2] = true;
}
}
// cout << endl;
}
}
int main()
{
while(cin >> N >> K){
memset(pos, false, sizeof(pos));
memset(step, 0, sizeof(step));
bfs();
cout << step[K] << endl;
}
return 0;
}