#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,x,ans;

int main()
{
	scanf("%lld%lld",&n,&x);
	n-=x; ans+=n+x;
	while(n%x)
	{
		ans+=(n/x)*2*x;
		n=n%x; swap(n,x);
	}
	ans+=(n/x-1)*2*x+x;
	printf("%lld\n",ans);
	return 0;
}