#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAXN 10001

ll n, x, ans;

void solve(ll nn, ll xx){
    xx = xx < (nn - xx) ? xx : (nn - xx);
    if(nn % xx == 1){
        ans += (nn / xx - 1) * xx * 3 + 3 * xx;
        printf("%lld\n", ans);
    }
    else {
        ans += (nn / xx - 1) * xx * 3;
        solve(nn - xx, nn % xx);
    }
}

int main()
{
    cin >> n >> x;
    x = x < (n - x) ? x : (n - x);
    if(n % x == 0){
        ans = (n / x - 1) * 3 * x;
        printf("%lld\n", ans);
    }
    else{
        ans = (n / x - 1) * x * 3;//辺x が n / x - 1個
        solve(n - x, n % x);
    }


    return 0;
}
