#include <iostream>
#include <string>
#include <queue>
#include <algorithm>

using namespace std;
#define X first
#define Y second

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;
    fill(dist,dist+100001,-1);
    q.push(n);
    dist[n]=0;
    while(dist[m]==-1) {
        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;
            
            dist[nx]=dist[cur]+1;
            q.push(nx);
        }
        
    }
    cout << dist[m];
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: