def gcd(a, b):
    if a > b:
        tmp = a
        a = b
        b = tmp

    if a == 0:
        return b

    else:
        return gcd(a, b%a)

n, x = map(int,input().split())

ans = 3 * (n - gcd(n, x))

print(ans)
