def sub(a, b):
    if (a%b == 0):
        return a//b-1
    return a//b

def mod(a, b):
    if a%b==0:
        return b
    return a%b

n, x = map(int, input().split(' '))
ans = n
c = [x, n-x]
while (c[0] != c[1]):
    c = [min(c), max(c)]
    ans += 2*sub(c[1], c[0])*c[0]
    c[1] = mod(c[1], c[0])
print(ans+c[0])
