#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
	long long n,x,ans;
	scanf("%lld%lld",&n,&x);
	ans=n;
	n-=x;
	while(x)
	{
		ans+=(n/x*x)<<1;
		n%=x;
		swap(n,x);
	}
	ans-=n;
	printf("%lld\n",ans);
}
/*
5 2
*/
