n,x = map(int,input().split())
ans = 0
if 2*x == n:
    ans = 3*x
else:
    ans += n
    a = x
    b = n - x
    while True:
        ans += (a//b)*2*b
        c = b
        b = a%b
        a = c
        if b == 0:
            ans -= a
            break
    
print(ans)
