n,x = list(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)
n,x = list(map(int,input().split()))

ans = n

e = n-x

while e > 0:

    if x%e == 0:    

        ans += x//e*(e*2)-e

    else:

        ans += (x//e)*e*2

    x,e = e,x%e    

print(ans)
n, x = list(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)

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

-count = 0 -while True: +while e > 0: -        break -    count += 1