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


def func(X, Y):
    if X < Y:
        return func(Y, X)
    # X>=Y
    if X % Y == 0:
        return 2*X-Y
    else:
        return 2*Y*(X//Y) + func(Y, X % Y)


ans = N + func(X, N-X)

print(ans)