题干:

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?

Input

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

Output

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.

Examples

Input


4 6

Output


2

Input


10 1

Output


9

Note

In 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.

 

解题报告:

   这题做法很多。。首先一眼就是老套的bfs,,就不说了。。。注意别忘判断出现小于0的情况就行了,,不然会RE的。。也就是你给他限制了上界别忘了限制下界就行了。(复杂度on的)

   还有一种方法就是倒着考虑这个问题,这个问题可以反过来说:我们应该用“对这个数加1”和“如果这个数是偶数,就除以2”的运算,得到从m开始的n。(复杂度logn的)

   因为你要知道啊如果m是个奇数,也就意味着我最终一定是先得到一个偶数然后再-1得到的,所以我们就加回去成个偶数,然后偶数要想得到n,毫无疑问只能/2,然后如果发现<n了,那就直接加回来就是答案了。这倒是不难想,因为你会发现 ÷2+1+1和+1÷2,得到的效果相同,但是后者仅用了两步,,所以说明这样贪心一定是时间最短的(相当于推出了bfs的策略,就可以不用bfs直接递推就可以了)。

  另外这个解法可以化简成dp的形式

AC代码1:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
char s[MAX];
int vis[50005];
int n,m;
ll mod = 1e9+7;
struct Node {
int x;
int t;
Node(int x,int t):x(x),t(t){}
};
int bfs() {
queue<Node> q;
q.push(Node(n,0));
while(!q.empty()) {
Node cur = q.front();q.pop();
if(cur.x == m) return cur.t;
if(vis[cur.x]) continue;
if(cur.x < 0) continue;
if(cur.x > 50005) {
printf("hahahhaha\n");
}
vis[cur.x]=1;
q.push(Node(cur.x-1,cur.t+1));
if(cur.x <= m) q.push(Node(cur.x*2,cur.t+1));
}
return -1;
}
int main()
{
cin>>n>>m;
if(n>=m) printf("%d\n",n-m);
else {
int ans = bfs();
printf("%d\n",ans);
}
return 0 ;
}

AC代码2:(思维)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int main()
{
int n,m;
cin>>n>>m;
if(n>=m) printf("%d\n",n-m);
else {
int ans = 0;
//这样正着推是错的、、、。。。。
// while(n*2<=m) {
// n*=2;ans++;
// }
// if(m%2 == 0) {
// printf("%d\n",ans+((n-m/2)+1));
// }
// else printf("%d\n",ans+1+n-m);
while(n!=m) {
if(m%2==1) ans++,m++;
m/=2;ans++;
if(n>=m) {
ans+=(n-m);break;
}
}
printf("%d",ans);
}
return 0 ;
}

 

AC代码3:(会慢一点,100ms左右)

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

int n, m, dp[2][20000], curr, last, cnt;

int main()
{
scanf("%d %d", &n, &m);
if(m < n){
printf("%d\n", n-m);
return 0;
}
dp[0][m] = 1;
last = 0;
curr = 1;
while(cnt<10000){
for(int i = 0; i<2*m; i++) {
if(dp[last][i]){
if(i == n){
printf("%d\n",cnt);
return 0;
}
if( i+1 < 20000 ) dp[curr][i+1] = true;
if( i%2 == 0 ) dp[curr][i/2] = true;
}
}
curr=!curr;
last=!last;
cnt++;
}

return 0;
}

AC代码4: