import sys
sys.setrecursionlimit(10 ** 9)
N, X = map(int, input().split())

def f(a, b):
    if a == b:
        return a
    if a >= b:
        a, b = b, a
    return 2 * a + f(a, b - a)

print (N + f(N - X, X))