#include <cstdio>

typedef long long ll;

ll n , x;

template < typename T >
inline T gcd(T a , T b) {
	return b ? gcd(b,a%b) : a;
}

int main() {
	
	scanf("%lld%lld",&n,&x);
	
	printf("%lld\n",3*n-3*gcd(n,x));
	
	return 0;
	
}