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

if N/2 == X:
    print(X*3)
elif N/2 > X:
    # 上側
    l = X + N - X
    l += ((N-X)//X)*2
    n = (N-X)%X
    l += (X//n)*2*n
    l += n
else:
    l = X
    n = X-N
    l += (N//n)*2*n
print(l)