#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll gcd (ll a, ll b){
    if (b==0) return a;
    return gcd(b, a%b);
}
int main() {
    ll N,X;
    scanf("%lld%lld", &N,&X);
    printf("%lld\n",3*(N-gcd(N,X)));
}