N, X = map(int, input().split())

ans = X
base = N - X
elem = X
while True:
    div, mod = divmod(base, elem)
    ans += div * elem * 2

    if mod == 0:
        ans += base
        break

    base = elem
    elem = mod


print(ans)