#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef long long ll;
#define all(c) begin(c),end(c)

ll f(ll a, ll b) {
    if (a == b)
        return a;

    if (a < b)
        return 2 * a + f(a, b - a);
    else
        return 2 * b + f(a - b, b);
}

auto solve() {
    ll N, X;
    cin >> N >> X;

    return N + f(N - X, X);
}

int main() {
    cout << solve() << endl;
    return 0;
}