#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) {
//    cerr << a << "," << b << endl;
    if (a == 0) return b;
    if (b == 0)return a;

    if (a == b)
        return a;

    if (a < b) {
        ll rem = b % a;
        ll d = b / a;
        return 2 * d + f(a, rem);
    } else {
        ll rem = a % b;
        ll d = a / b;
        return 2 * d + f(rem, b);
    }

}

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

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

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