#include<cstdio>
#include<algorithm>
using namespace std;
#define LL long long
LL N,X,Ans;
LL dz(LL a,LL b){
	if(a==0)return -b;
	LL ret=(b/a)*2*a;
	ret+=dz(b%a,a);
	return ret;
}
int main(){
	scanf("%lld%lld",&N,&X);
	printf("%lld\n",dz(X,N-X)+N);
}
