#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long n, x; cin >> n >> x;
    long long m = max(x, n - x);
    long long d = __gcd(x, n);

    if (n % 2 == 0) cout << m * 3 << '\n';
    else cout << 3 * (n - d) << '\n';
    return 0;
}
