#include<iostream>
#include<vector>
#include<algorithm>

#define REP(i,n) for(int i=0; i<n; ++i)
using namespace std;
typedef long long int ll;
int main(){
    ll n, x;
    cin >> n >> x;
    x = min(x, n-x);
    if(n%x){ cout << 3*(n-1) << endl; }
    else   { cout << 3*(n/x-1)*x << endl; }

    return 0;
}
[yuri] 16-07-16 21:37:43> ~/agc
