#include <cstdio>
typedef long long LL;
LL N, K; 
LL Gcd(LL A, LL B) {
	if(!B) return A;
	return Gcd(B, A % B);
}
int main() {
	scanf("%lld%lld", &N, &K);
	LL M = N - K;
	if(M == K) printf("%lld", 3 * K);
	else printf("%lld", (N - Gcd(N, K)) * 3);
}