#include<cstdio>
using namespace std;
long long n,x,ans,a,b;
int main()
{
	scanf("%lld%lld",&n,&x);
	ans=n;n=n-x;
	while(x) {
		ans+=n/x*x*2;
		a=n%x;b=x;
		if(!a) ans-=x;
		n=b;x=a;
	}
	printf("%lld",ans);
	return 0;
}