#include <bits/stdc++.h>
using ll = long long;
using namespace std;

ll gcd(ll a, ll b)
{
    return (b == 0? a : gcd(b, a % b));
}

int main()
{
    ll n, x; cin >> n >> x;
    cout << 3 * (n - gcd(n, x)) << '\n';
}