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

ans = n

longer = max(x, n-x)
shorter = min(x, n-x)

while True:
    m = longer // shorter
    l = longer % shorter
    ans += m*shorter*2
    if l == 0:
        ans -= shorter
        break
    longer = shorter
    shorter = l
    
print(ans)