N, X = map(int, input().split())
ans = N
a, b = max(X, N-X), min(X, N-X)
while b:
    q, r = divmod(a, b)
    if r == 0:
        ans += b * (2 * q - 1)
    else:
        ans += b * 2 * q
    a, b = max(b, r), min(b, r)
print(ans)