#B
n,x = [int(i) for i in input().split()]
ans = x
a = x
p = n-x
while(1):
    q,r = divmod(a,p)
    ans += p * 2* q
    if r==0:
        print(ans)
        break
    else:
        ans += p
        ans += r
        a = p -r 
        p = r        