N, X = [int(elem) for elem in input().split()]
total_distance = N
bigger, smaller = N - X, X
while bigger % smaller != 0:
    total_distance += 2 * (bigger // smaller) * smaller
    bigger, smaller = smaller, bigger % smaller

total_distance += 2 * (bigger // smaller) * smaller - smaller
print(total_distance)
