#include<cstdio>
#define ll long long
ll solve(ll n,ll x)
{
	if (n%x==0) return 2*n-x;
	return solve(x,n%x)+n/x*x*2;
}
int main()
{
	ll n,x;
	scanf("%lld%lld",&n,&x);
	printf("%lld",solve(n-x,x)+n);
}