N, X = map(int, input().split())
a = max(N - X, X)
b = min(N - X, X)
ans = N
while b != 0:
    q = a // b
    r = a % b
    ans += 2 * b * q
    if r == 0:
        ans -= b
    a = b
    b = r
print(ans)
