#include<cstdio>
using namespace std;
#define ll long long
ll n,x;
ll gcd(ll a,ll b){
	if(a%b==0)return 3*a;
	else return gcd(b,a%b)+(a/b)*3*b;
}
int main(void){
	scanf("%lld%lld",&n,&x);
	printf("%lld",gcd(n-x,x));
	return 0;
}