#include<cstdio>
typedef long long LL;

LL n,x;

LL Gcd(LL a,LL b) {
	if(b==0)
		return -a;
	else
		return Gcd(b,a%b)+(a/b)*2*b;
}
int main() {
	scanf("%lld %lld",&n,&x);
	printf("%lld\n",Gcd(n-x,x)+n);
	return 0;
}