def gcd(a, b):
        while a != b:
                if a > b :
                        a -= b
                else:
                        b -= a
        return a

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

print(3*(N - gcd(N, X)))
