# #include <stdio.h>
# #include <string.h>
# #include <stdlib.h>

# typedef unsigned long long ull;
# typedef   signed long long sll;


# int comp (const void *x, const void *y) {
# 	return *(int*)x - *(int*)y;
# }

# int min (int x, int y) {
# 	return (x < y) ? x : y;
# }

# int solve (ull x, ull y) {
# 	printf("%llu %llu\n", x, y);
# 	if (x > y) {
# 		return (x / y) * 2 + solve(x % y, y);
# 	} else if (x == 0) {
# 		return 0;
# 	} else {
# 		ull s = y / x;
# 		return s * (x + y) - (s * (s-1) / 2) * x + solve(y % x, x);
# 	}
# }

# int main (void) {
# 	ull n, x;
# 	scanf("%llu%llu", &n, &x);
# 	// if ((x << 1) == n) {
# 	// 	printf("%llu\n", 3 * x);
# 	// } else {
# 	// 	printf("%llu\n", 6 * x);
# 	// }
# 	printf("%llu\n", x + solve(x, n-x));
# 	return 0;
# }

def solve (x, y)
	if x > y then
		(x / y) * y * 2 + solve(x % y, y)
	elsif x == 0
		0
	else
		s = y / x
		s * (x + y) - (s * (s-1) / 2) * x + solve(y % x, x)
		# solve(y-x, x)
	end
end

$n, $x = gets.chomp.split(' ').map{|i| i.to_i(10)}

puts ($x + solve($x, $n - $x))
