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

ans = n
e = n-x
count = 0
while True:
    
    if x%e == 0:    
        ans += x//e*(e*2)-e
        break
    else:
        ans += (x//e)*e*2
    x,e = e,x%e
    count += 1
    
print(ans)