import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,x = map(int, input().split())
v0 = n
v1 = n-x
ans = x
init = 0
while True:
    num, amari = divmod(v0,v1)
#     print(ans, v0,v1)
    if amari==0:
        ans += (2*(num-1)+init)*v1
        break
    else:
        ans += (2*(num-1)+1+init)*v1
        v0,v1 = v1,amari
    if init == 0:
        init = 1
print(ans)