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

if X * 2 == N:
    ans = 3 * X
else:
    ans = 0
    if X * 2 < N:
        LL = N - X
        SS = X
    else:
        LL = X
        SS = N - X
    while True:
        div, mod = divmod(LL, SS)
        ans += 3 * SS * div
        if mod == 0:
            break
        LL = SS
        SS = mod

print(ans)