n , x = map(int,(input().split()))
a , b = n-x , x

ans = n
while True:
    c = max(a,b)
    d = min(a,b)
    if c % d != 0:
        ans += (c // d) * 2 * d
    else:
        ans += ((c // d) * 2 - 1) * d 
        break
    
    a = d
    b = c%d
    
print(ans)