#include<bits/stdc++.h>
#define INF 1<<64
#define rep(a,b,c) for(int a=b;a<c;a++)
#define pb push_back
#define int long long int
#define str string
using namespace std;
int n,m,a=0;
vector<int> v,ans;
int keisan(int wra, int wru){
    if((wra%wru)==0)a=0;
    if(wra==0){//cout << "a";
        return (-1)*(wru-wra);
        // return 0;
    }else if(wra<wru)return keisan(wru,(wru-wra));
    else {
           // cout << wra <<" " << wru;
        return keisan((wra%wru),wru)+(wra/wru)*wru*2;

    }
}
signed main(){
    cin >> n >> m;
    if(n/2==m&&n%2==0){
        a=m*3;
    }else if(n/2>=m){
        a=keisan((n-m),m);
        //cout << a;
        a+=m+(n-m);
    }else{
        a=keisan(m,(n-m));
       // cout <<a;
        a+=m;
    }
    cout << a << endl;
return 0;
}
