N, X = map(int, input().split())
if 2 * X > N:
    X = N - X

# if N == 2 * X:
#     print (3 * X)
#     exit()

# if X == 1:
#     print (3 * X * (N - 1))
#     exit()

# if X == 2:
#     print (3 * X  * 2)
#     exit()

x = 3 * X
n = (N - 1)//X
print (x * n)
