N, x = [int(s) for s in input().split()]

l = N

n = max(x,N-x)
x = min(x,N-x)

while (True):
    q = n // x
    r = n % x
    if r == 0:
        print(l + (2*q-1)*x)
        break
    
    l += (2*q)*x
    n = x
    x = r