#include <iostream>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
#define X first
#define Y second
int board[100002];
int dist[100002];
int dx[3]={1,-1,2};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n,m;
queue<int> q;
cin >> n >> m;
//예외
if(n==m){
cout<< 0;
return 0;
}
fill(dist,dist+100002,-1);
q.push(n);
dist[n]=0;
while(!q.empty()) {
int cur = q.front();
q.pop();
for(int i=0;i<3; i++) {
int nx;
if(i==2) nx=cur*dx[i];
else nx=cur+dx[i];
if(nx<0 || nx>100000) continue;
if(dist[nx]!=-1) continue;
if(nx==m) {
cout << dist[cur]+1 ;
return 0;
}
dist[nx]=dist[cur]+1;
q.push(nx);
}
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: