def compute(n, x):
    quo = n // x
    rem = n % x
    if rem is 0:
        return (n - 1) * 3
    else:
        return (2*quo - 1) * x + (n - x) + compute(x, rem)

n, x = map(int, input().split())
print(compute(n, x))