#include<cstdio>
#include<cmath>
#define f(i,a,b) for(register int i=(a);i<=(b);i=-(~(i)))
typedef long long ll;
ll n,x;
ll gcd(ll a,ll b){return !b?a:gcd(b,a%b);}
int main()
{
	scanf("%lld%lld",&n,&x);
	printf("%lld\n",3*(n-gcd(n,x)));
	return 0;
}