n,x=map(int,input().split())
if x>n/2:
    x = n-x
ans = n
c = x
d = n-x
while c > 0:
    if d%c==0:
        ans += int(d/c)*(2*c) - c
    else:
        ans += int(d/c)*(2*c)
    c,d = d%c,c
print(ans)
