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

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

int main(){
    ll n, x;
    cin >> n >> x;
    cout << 3 * (n - gcd(n, x)) << endl;;
    return 0;
}