#include <cstdio>
typedef long long LL;
LL f(LL a, LL b)
{
	if(!b) return -a;
	return a/b*b*2 + f(b, a%b);
}
int main()
{
	LL N, X, ans;
	scanf("%lld%lld", &N, &X);
	ans = N + f(N-X, X);
	printf("%lld\n", ans);
}
