#include <iostream>

using namespace std;

typedef long long ll;

ll F(ll a, ll b)
{
    if( b % a == 0 ) return 2 * b - a;

    return 2 * a * ( b / a ) + F(b % a, a);
}

int main()
{
    ll n, x;
    cin >> n >> x;
    ll a = x, b = n - x;
    if(a > b)swap(a, b);
    cout << n + F(a, b) << '\n';
    return 0;
}
