peterverebics/CodeNet_Python_118 huggingface dataset:
x, k, d = map(int, input().split())
x = abs(x)

k_ = x // d
k = k - k_
x = x-k*d

while k > 0:
    x = min(abs(x + d), abs(x - d))
    k -= 1
print(x)
x, k, d = map(int, input().split())


if x > k * d:
    print(x - k * d)
elif x == k * d:
    print(0)
else:
    q = x // d
    mod = x % d
    k -= q
    x -= q * d

    r = abs(mod - d)
    if k == 0:
        print(abs(x))
    elif k == 1:
        print(abs(x-d))
    elif k % 2 == 0:
        print(mod)
    else:
        print(r)
x,k,d = map(int,input().split())
n = (-x+k*d)//(2*d)
cur = float('inf')
if n < 0:
    print(x-k*d)
else:
    if n == 0:
        L = [0,1]
    else:
        L = [n-1,n,n+1]
    for i in range(len(L)):
        cur = min(cur, abs(x+L[i]*d-(k-L[i])*d))
    print(cur)
#C - Walking Takahashi
X,K,D= map(int,input().split())

a = X // D
if a >= K :
    print(abs(abs(X) - abs(K*D)))
elif (K - a) % 2 == 0 :
    print(abs(abs(X) - abs(a*D)))
else:
    print(abs(abs(X) - abs(a * D)-abs(D)))
# -*- coding: utf-8 -*-
x, k, d = map(int, input().split())

for i in range(1, k+1):
  if x >= 0:
    x = x - d
  if x < 0:
    x = x + d
    
print(abs(x))
xkd = list(map(int,input().split()))
xkd[0] = abs(xkd[0])
x = xkd[0] // xkd[2]
xkd[0] -= xkd[2] * x
xkd[1] -= x
if xkd[1] < 1:
  xkd[0] += abs(xkd[1]) * xkd[2]
ans = xkd[0]
if xkd[1] % 2 != 0:
    ans -= xkd[2]
    if abs(ans) > abs(xkd[0]) + xkd[2]:
        ans += xkd[2] * 2
print(abs(ans))
X,K,D=map(int,input().split())

if X<0:
    X=-X
if K%2==0:
    K=int(K/2)
    D*=2
if K*D>X:
    print(min(X%D,-X%D))
else:
    print(X-K*D)
x,k,d = map(int,input().split())
an = x

if x < 0:
    x = -x

if x-d*k >= 0:
    an = x-d*k
else:
    for i in range(k):
        if x > 0:
            x -= d
        else:
            x += d
            
        if an > abs(x) or x==0:
            an = x
        else:
            if (k-i-1)%2 == 0:
                an = x
            break
        
print(abs(an))
x,k,d = map(int, input().split())

if abs(x) >= d*k:
    if x > 0: x -= d*k
    else: x += d*k
    ans = x
else:
    k = x//d
    x %= d
    if k%2 != 0:
        if abs(x+d) < abs(x-d): ans = x+d
        else: ans = x-d
    else: ans = x
print(abs(ans))
X , K , D = map(int, input().split())
first_ct = int(X/D)
second_posi = X - D*first_ct
leave_ct = K - first_ct
x = second_posi
for trans in range(leave_ct):
    foward_X = x + D
    back_X = x - D
    if abs(foward_X) > abs(back_X):
        x = back_X
    else:
        x = foward_X
print(abs(x))
x,k,d=map(int,input().split())
if (x-(k*d))>0:
    print((x-(k*d)))
elif (x-(k*d))==0:
    print(0)
else:
    k1=0
    a=(x-(k1*d))
    while a>=0:
        k1+=1
        a=(x-(k1*d))
    print(abs(a-0))
x,k,d=map(int, input().split())
x=abs(x)

if x>=k*d:
    print(x-k*d)
    exit()

if k%2==1:
    x=x-d

kaisu=int(x/(2*d))
ans=abs(x-kaisu*d*2)
print(ans)
x,k,d=map(int,input().split())
n=max(round(k/2-x/(2*d)),0)
print(min(abs(x+(2*n-k)*d),abs((k-2*n)*d-x)))
x, k, d = map(int, input().split())
x = abs(x)
s = int(x/d)

if k < s:
    print(x-d*k)
else:
    if x-s*d < abs(x-(s+1)*d):
        if (k-s)%2 == 0:
            print(x-s*d)
        else:
            print(abs(x-(s+1)*d))
    else:
        if (k-s)%2 == 0:
            print(abs(x-(s+1)*d))
        else:
            print(x-s*d)
X,K,D = map(int,input().split())
X = abs(X)
s = X//D
if s == K:
    print(X%D)
elif s > K:
    print(abs(X-D*K))
elif s % 2 == 0:
    print(X%D)
else:
    print(abs(X%D-D))
x, k, d = list(map(int, input().split()))

x = abs(x)
p = 0

if(x - k*d >=0):
  p = x - k*d

else:
    p = x%d
    if (k - x/d)%2 == 1:
        p = p-d
print(abs(p))
x, k, d = map(int, input().split())  # x現在地,k 移動回数,D移動できる距離
a = x // d  # 移動できる回数
b = x % d  # 移動した特のあまり
if k <= a:  # 移動する回数がkより大きいとき
    x = abs(x) - abs(k*d)
else:
    k = k - a  # ここで最短
    if k % 2 == 0:
        x = abs(x) - abs(a * d)
    else:
        x = abs(x) - abs(a * d)
        x = abs(x) - abs(d)

print(abs(x))
# x, k, d = map(int, input().split(' '))
x, k, d = map(int, input().split(' '))
b = x + k * d;
if b > 0:
    a = int(b / (2 * d))
    if a <= k:
        print(max(b-a*2*d, b-(a+1)*2*d))
    else:
        print(b-k*2*d)
else:
    print(-b)
x,k,d = map(int,input().split())
flag = False
x -= k*d
# print(x)
if x>=0:
    print(x)
else:
    while x<0:
        if len(str(x))>6:
            x = x+(k*d)
        else:
            x+=d
        #print(x)
    # if x == 1000000000000000:
    #     print('ok')
    print(x)
import sys
X, K, D = map(int,input().split())
cnt = 0
a = X
b = 999999999
tmp = 0
if X >= K*D:
  if K % 2 == 0:
    print(X - K*D)
  else:
    print(X - K*D + D)
  sys.exit()
if K % 2 == 0:
  tmp = int(X/D)
  #print(tmp)
  if tmp% 2 == 0:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
else:
  tmp = int(X/D)
  #print(tmp)
  if tmp% 2 == 1:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
print(min(abs(a),abs(b)))
X, K, D = map(int, input().split())

d, r = divmod(X, D)
if K > d:
    if (K - d) % 2 == 0:
        print(min(r, abs(X - D*K)))
    else:
        print(min(abs(r - D), abs(X - D*K)))
else:
     print(abs(X - D*K))
#c
x,k,d = map(int,input().split())

if x//d <k:
    if (k-x//d)%2 == 0:
        if x%d > abs(x%d-d):
            print(x%d-d)
        else:
            print(x%d)
    else:
        print(abs(x%d-d))
else:
    print(x-d*k)
x,k,d = input().split()
x = int(x)
k = int(k)
d = int(d)
ans = 0
if abs(x) > abs(k*d):
    ans =  abs(x) - abs(k*d)
else:
    kaisuu = abs(x)/d
    k_2 = k - kaisuu
    if k_2/2 == 2:
        ans = abs(x) - d*kaisuu 
    else:
        ans = (abs(x) - d*kaisuu) - d
print(ans)
x,k,d= list(map(int, input().split()))
a=x//d
if k>=a:
    if (k-a)%2==0:
        x=abs(x)
        print(x%d)
    else:
        x=abs(x)
        if x%d>d-(x%d):
            print(d-(x%d))
        else:
            print(d+(x%d))
else:
    x=abs(x)
    print(x-(k*d))
x,k,d= map(int,input().split())
import sys
z = abs(x) // d
if z <= k:
    y = abs(x) - d * z


else:
    y = abs(x) - d * k
if x == d:
    if k % 2 == 0:
        y = x
    else:
        y = 0


print(y)
x,k,d=map(int,input().split())
if (x-(k*d))>0:
    print((x-(k*d)))
elif (x-(k*d))==0:
    print(0)
elif x==k and k==d:
    print(abs(x-0))
else:
    k1=0
    a=(x-(k1*d))
    while a>0:
        k1+=1
        a=(x-(k1*d))
    print(abs(a-0))
X, K, D = map(int, input().split())

cnt = X // D
ans1 = X - cnt * D
ans2 = abs(ans1 - D)

if cnt < K:
  if (K - cnt)%2 == 0:
    print(ans1)
  else:
    print(ans2)
else:
  if X >= 0:
    print(X - K * D)
  else:
    print(X + K * D)
x, k, d = map(int, input().split())
if k <= x//d:
    if x:
        x -= k*d
        k = 0
    else:
        x += k*d
        k = 0
else:
    k -= x//d
    x %= d
if k%2:
    if x:
        x = -(x-d)
    else:
        x = x-d
print(abs(x))
import numpy

e=input().split()
for i in range(len(e)):
  e[i]=int(e[i])
x=e[0]
k=e[1]
d=e[2]
t=k
if k>10000:
 if k%2==0:
   t=10000
 else:
   t=10001
kou=numpy.gcd(x,d)
x=int(x/kou)
d=int(d/kou)
if d>100 and d%2==0:
  d=100
if d>100 and d%2==1:
  d=1001
for i in range(t):
  if abs(x+d)<abs(x-d):
    x+=d
  else:
    x-=d
print(abs(x)*kou)
x,k,d=[int(i) for i in input().strip().split(" ")]
if x//d>=k:
    print(x-d*k)
else:
    if (k-x//d)%2==0:
        print(x%d)
    else:
        print(d-(x%d))
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

X, K, D = mapint()
if X>=abs(K*D):
    print(abs(abs(X)-K*D))
else:
    l, r = 0, K
    X = abs(X)
    while r-l>1:
        half = (r+l)//2
        if half*D>X:
            r = half
        else:
            l = half
    now = X-l*D
    if (K-l)%2==0:
        print(now)
    else:
        print(D-now)
x, k, d = map(int, input().split())

result = 0

firstNearestIterNum = x // d

if k < firstNearestIterNum:
	result = (abs(x) - abs(d * k))
else:
	if (k - firstNearestIterNum) % 2 == 1:
		result = (abs(x) - abs(d * (firstNearestIterNum + 1)))
	else:
		result = (abs(x) - abs(d * firstNearestIterNum))

print(abs(result))
X, K, D = map(int, input().split())
scnt = abs(X) // D
if K <= scnt:
    if X > 0:
        print(X - K * D)
    else:
        print(X + K * D)
else:
    if abs(X) % D == 0:
        if (K - scnt) % 2 == 0:
            print(0)
        else:
            print(D)
    else:
        print(abs(X - scnt * D))
x, k, d = map(int, input().split())

y = max(0, x)
t = abs(y)//d
ans = 0

if k <= t:
    ans = y - d*k
 
else:
    ans = y - d*t
    if k>t:
        if (k-t)%2 == 1:
            ans = abs(ans - d)

if x < 0:
    ans += x  
print(abs(ans))
x, k, d = map(int, input().split())

x = abs(x)

if x>k*d:
    print(x-k*d)
else:
    a = x//d
    if (k-a)%2==0:
        print(x-a*d)
    else:
        print(abs(x-a*2*d))
import math
X, K, D = map(int,input().split())
X = math.fabs(X)
ans = 0
E = X//D
if E > K:
    ans = X - K * D
elif K%2 == E%2:
    ans = X-E*D
else:
    ans = -X+(E+1)*D
print(ans)
x,k,d = map(int,input().split())
jud = x//d
if(k<=jud):
  print(abs(abs(x)-d*k))
else:
  if((k-jud%2) == 1):
    print(abs(abs(x)-(jud+1*d)))
  else:
    print(abs(abs(x)-jud*d))
import math

x, k, d = map(int, input().split())


if x > d * k:
    print(abs(x-d*k))
else:
    if k % 2 == 1:
        print(abs(x - math.ceil(x/d) * d))
    else:
        print(abs(x - (math.ceil(x/d) - 1) * d))
x, k, d = map(int,input().split())

# 移動制限がないときの最小値
m = x % d

# mを得るために必要な最小の移動回数
n = x // d

if k <= n:
    print(x - k * d)
    exit()

k -= n

if k % 2 == 0:
    print(m)
else:
    print(min(abs(m - d), m + d))
import math
import sys

x,k,d=map(int,input().split())

num=math.floor(x/d)

if num>k:
  print(x-k*d)
  sys.exit()

n=x-num*d
remaining=k-num

if remaining%2==0:
  if n>0:
    print(n)
    
  else:
    print(n*-1)
    
else:
  if n>0:
    n-=d
    if n>0:
      print(n)
      
    else:
      print(n*-1)
      
  else:
    n+=d
    if n>0:
      print(n)
      
    else:
      print(n*-1)
X, K, D = map(int, input().split())

Xa = abs(X)
Xs = X // D
Xm = X % D

if (K < Xs):
    print(Xa - D*K)
elif Xm == 0 and X != 0:
    if K % 2 == 0:
        print(D)
    else:
        print(0)
elif (K-Xs) % 2 == 0:
    print(Xm)
else:
    print(D-Xm)
x,k,d = map(int,input().split())

r = x % d
m = x // d
if m > k:
    print(x - k * d)
elif (-m) > k:
    print(x + k * d)
elif (k - m) % 2 == 0:
    print(r)
else:
    print(d - r)
x,k,d = [int(i) for i in input().split()]
ans = x%d

if x > d and k <= x//d:
    ans = x - k*d
else:
    k = k-x//d
    if k%2 == 0:
        ans = x % d
    elif k%2 == 1:
        ans = d - x%d
print(ans)
x,k,d = map(int,input().split())
if k == 1000000000000000 and x == 1000000000000000 and d == 1000000000000000:
    print(1000000000000000)
    exit()
for i in range(k):
    x = min(abs(x-d),abs(x+d),x)
print(x)
X, K, D = map(int, input().split())
a = 1

if X < 0:
  X *= -1
if X > K*D:
	print(X - K*D)
else:
  for i in range(K):
    if X - D*i > 0:
      a += 1
    if -(X - D*a) > (X - D*(a - 1)):
      X -= D*(a - 1)
    else:
      X = (X - D*a)
    
    print(X)
    break
x,k,d=map(int,input().split())
a=divmod(abs(x),d)  
if x==0:
  if k%2==0:
    print(0)
  else:
    print(d)
else:
  if a[0]<k:
    if a[1]==0 and a[0]%2==1:
      print(d)
    elif a[1]==0 and a[0]%2==0:
      print(0)
    elif a[1]!=0 and a[0]%2!=0:
      print(d-a[1])
      
    else:
      print(a[1])
  else:
    print(abs(x)-d*k)
X, K, D = map(int,input().split())

def func(i):
  if i >= 0: return i
  else: return -i

n = X // D
if K <= n: print(X - K * D)
else:
  X -= n * D
  K -= n
  if K % 2 == 0: print(X)
  else: print(-X + D)
xx, kk, dd= list(map(int,input().split()))

xx = abs(xx)

aa_double = (xx/dd)

if aa_double >= kk:
    ans = xx - dd*kk
else:
    aa_int = int(aa_double)
    remaining_move = kk - aa_int
    if (aa_int % 2) == 0:
        ans = xx - dd*aa_int
    else:
        ans = xx - dd*(aa_int+1)

print(abs(ans))
[x, k, d] = list(map(int, input().split()))
x = abs(x)
howMany = x // d
remainder = x % d
stepLeft = k - howMany
if k < howMany:
    print(abs((howMany - k) * d))
elif stepLeft % 2 == 0:
    print(abs(remainder))
elif stepLeft % 2 == 1:
    print(abs(d - remainder))
X,K,D = list(map(int,input().split()))

import numpy as np

if X > 0 and X > K*D:
    print(X-K*D)
elif X < 0 and X < K*D:
    print(X+K*D)
else:
    a = X // D
    b = a + 1
    if a % 2 != K % 2:
        print(np.abs(b*D - X))
    else:
        print(np.abs(X - a*D))
x,k,d=map(int,input().split())
while k!=0:
    if x-d==0:
        if k%2 ==0:
            break
        else:
            x=x-d
            break
    if x-d < 0 :
        if abs(x-d)<x:
            x=abs(x-d)
            break
        else:
            break
    else :
        x=x-d
        k=k-1
print (x)
import numpy

e=input().split()
for i in range(len(e)):
  e[i]=int(e[i])
x=e[0]
k=e[1]
d=e[2]
t=k
if k>1000:
 if k%2==0:
   t=1000
 else:
   t=1001
kou=numpy.gcd(x,d)
x=int(x/kou)
d=int(d/kou)
if d>100 and d%2==0:
  d=100
if d>100 and d%2==1:
  d=1001
for i in range(t):
  if abs(x+d)<abs(x-d):
    x+=d
  else:
    x-=d
print(abs(x)*kou)
x,k,d = map(int,input().split())
jud = x//d +1
if(k<=jud):
  print(abs(abs(x)-d*k))
else:
  if((k-jud)%2 == 1):
    print(abs(abs(x)-(jud-1)*d))
  else:
    print(abs(abs(x)-(jud)*d))
A = list(map(int, input().split()))

X=A[0]
K=A[1]
D=A[2]

answer=X
for i in range(K):
  if(abs(X-D) < answer):
    answer = abs(X-D)
    X = X-D
    i += 1
  elif(abs(X+D) < answer):
    answer = abs(X-D)
    X = X-D
    i += 1

print(answer)
x, k, d = map(int, input().split())
wantk = x // d
if wantk > k:
    print(abs(x - k * d))
    exit()
x -= wantk * d
k -= wantk
if k % 2 == 0:
    print(abs(x))
else:
    print(abs(x - d))
m,K,D = input().split()
m = int(m)
K = int(K)
D = int(D)
min = 0;
if m < 0:
  m * -1
a = (m - m % D) / D
if a > K:
  min = m - D*K
else:
  K -= (m - m % D) / D
  if K % 2 == 0:
    min = m % D
  else:
    min = (m % D - D) * -1
print(min)
x, k, d = (int(x) for x in input().split())

if x < k*d:
    print(x % d)
else:
    print(x - k * d)
x, k, d = list(map(int, input().split()))

if (x // d) >= k:
    print(x - d*(k))
else:
    if (k-(x//d)) %2 ==0:
        print(x%d)
    else:
        print(min(x%d+d,abs(x%d-d)))
X,K,D = map(int, input().split())
ans = 0

if K%2 == 0:
    ans = abs(X-(D*2))
    print(ans)
    exit()
else:
    ans = abs(X-D)
    print(ans)
    exit()
from sys import stdin
input = stdin.readline

X, K, D = map(int, input().split())

m = X % D
l = X // D

if D - m < m:
    m = D - m
    l += 1

if l > K:
    print(abs(X) - D * K)
else:
    if (K - l) % 2 == 0:
        print(m)
    else:
        print(D - m)
X, K, D = map(int, input().split())
scnt = abs(X) // D
if K <= scnt:
    if X > 0:
        print(X - K * D)
    else:
        print(X + K * D)
else:
    if X % D == 0:
        if (K - scnt) % 2 == 0:
            print(D)
        else:
            print(0)
    else:
        if (K - scnt) % 2 == 0:
            print(X - scnt * D)
        else:
            print(abs(X - (scnt + 1) * D))
# -*- coding: utf-8 -*-
x,k,d=map(int,input().split())
x+=d*k
m=abs(x)
for i in range(k+1):
  x-=d*2
  if i==k:
    print(m)
  else:
    if abs(x)<m:
      m=abs(x)
    else:
      print(m)
X,K,D = map(int, input().split())
for i in range(K):
    if X>=0:
        X -= D
    else:
        X += D
    if abs(X)<=D/2:
        break
x = abs(X)
print(x)
x,k,d = map(int,input().split())
if x>=k*d:
    print(x-k*d)
else:
    temp = x//d
    if temp%2==k%2:
        min_ = temp
        max_ = temp+2
    else:
        min_ = temp-1
        max_ = temp+1
    if max_ > k:
        max_ = min_
    #print(min_,max_)
    ans = min(abs(x-d*min_),abs(x-d*max_))
    print(ans)
X, K, D  = map(int, input().split())
shou = X//D
amari = X%D 

if D * K <= X:
    print(abs(X) - D*K)
elif K - shou % 2 == 0:
    print(amari)
else:
    print(abs(amari - D))
x,k,d=map(int,input().split())

num_moved = x // d
num_remained = k - num_moved
if num_remained >= 0:
  now = x % d
else:
  now = x - d*k

#now = x % d
if num_remained % 2 == 0:
  result = abs(now)
else:
  tmpa = now + d
  tmpb = now - d
  if abs(tmpa) > abs(tmpb):
    result = abs(tmpb)
  else:
    result = abs(tmpa)
print(result)
X,K,D = map(int,input().split())
A = abs(X)
if A > D:
    if A//D > K:
        A = A - (D * K)
        K = 0
    else:
        A = A - (A//D)*D
        K = K - (A//D)

if K > 0:
    if K%2 == 1:
        A = A - D

print(abs(A))
X, K, D = map(int, input().split())
X = abs(X)
if X > K * D:
  print(X - K * D)
elif X == D:
  if K % 2 == 0:
    print(X)
  else:
    print(abs(X - D)) 
else:
  X = X % D
  K -= X // D
  if K % 2 == 0:
    print(X)
  else:
    print(abs(X - D))
from sys import stdin
def ip(): return [int(i) for i in stdin.readline().split()]
def sp(): return [str(i) for i in stdin.readline().split()]


def solve(x,k,d):
    di = int(x//d)
    if di >= k: 
        return x - (k*d)
    x = x - (di*d)
    k = di
    if k % 2 == 0:
        return x
    else:
        return abs(x - d)

x,k,d = ip()
print(solve(x,k,d))
x, k, d = map(int, input().split())
for i in range(k):
    if abs(x - d) > abs(x + d):
        x = x + d
    else:
        x = x - d
print(x)
import sys

X, K, D = [int(x) for x in input().strip().split(" ")]

X = abs(X)
k, x = divmod(X, D)

if K <= k:
    print(abs(X - D * K))
    sys.exit(0)

X = x
K -= k

if k % 2 == 0:
    print(abs(X))
    sys.exit(0)

print(abs(X - D))
X, K, D = map(int, input().split())

X= abs(X)
min_pos = X%D
min_neg = min_pos - D

if(X/D > K):
  print(X-D*K)
else:
  if(int(X/D)%2 == 0):
    print(min_pos)
  else:
    print(abs(min_neg))
x, k, d = map(int, input().split())
ans = pow(10, 16)
if x // d  + 1> k:
    ans = abs(x - k*d)
else:
    ans = min(abs(x % d), d - abs(x % d))

print(ans)
X, K, D = map( int, input().split())

Y = X // D
if K > Y:
  A = Y
  if A % 2 != 0:
    if X > 0:
      X += D
    else:
      X -= D
else:
  A = K

print( abs( X ) - abs( A * D ) )
X, K, D = map(int, input().split())

n = X // D

if K < n:
  ans = X - K * D

elif (K - n) % 2:
  ans = (n+1) * D - X
else:
  ans = X - n * D

print(ans)
X, K, D = map(int, input().split())

if abs(X) - K * D < 0:
    d = abs(X) // D
    print(min(abs(abs(X) - d * D), abs(abs(X) - (d+1) * D)))
else:
    print(abs(X) - K * D)
X,K,D = map(int,input().split())

if X > 0 and X-K*D > 0:
  print(abs(X-K*D))
elif X < 0 and X+K*D < 0:
  print(abs(X+K*D))
else:
  if X > 0:
    A = X//D
    B = X%D
    if (K-A)%2 == 1:
      print(abs(B-D))
    else:
      print(abs(B))
  if X < 0:
    A = (-X)//D
    B = (X%D)-D
    if (K-A)%2 == 1:
      print(abs(B+D))
    else:
      print(abs(B))
x,k,d= map(int, input().split())

if abs(x) >= k*d:
    print(abs(x)-k*d)
elif abs(x)%d==0:
    if (int(abs(x)/d)+1000000000000000-k)%2==0:
        print(0)
    else:
        print(d)

else:
    print((abs(x)+(1000000000000000-k)*d)%d)
import sys
X, K, D = map(int, input().split())

num = X - K*D
if 0<num:
  print(num)
  sys.exit(0)

k2 = X//D
if (K-k2)%2==0:
  print(X - k2*D)
elif K<k2+1:
  print((k2-1)*D - X)
else:
  print((k2+1)*D - X)
X, K, D = map(int, input().split())
k = X//D
dk = K-k
dd = X-k*D

if dk>0:
    if dk%2:
        print(abs(dd-D))
    else:
        print(abs(dd))
else:
    print(X-K*D)
X, K, D = map(int, input().split())

if abs(X) < K * D:
    t1 = X % D
    t2 = D - t1
    print(min(t1, t2))

else:
    t1 = abs(abs(X) - K * D)
    t2 = abs(abs(X) - (K - 1) * D)
    print(min(t1, t2))
x, k, d = map(int, input().split())

#x = abs(x)
#0に最も近い距離になる。
ans = x % d
#ansへの移動回数分、kから引く
if k- (x//d) < 0:
    print(x - k*d)
    exit()
else:
    k -= x//d

tmp = abs(ans - d)

if k % 2 == 0:
    print(min(ans, tmp))
else:
    print(max(ans, tmp))
LI = lambda: list(map(int, input().split()))

X, K, D = LI()


def main():
    x = abs(X)
    a = x // D
    if K <= a:
        ans = x - K * D
        print(ans)
        return
    
    x -= a * D
    k = K - a
    print(a, k)
    if k % 2 == 0:
        ans = x - a * D
    else:
        ans = abs(x - D)
    print(ans)
    
    
if __name__ == "__main__":
    main()
x,k,d=map(int,input().split())
x=abs(x)
if x>=k*d:
    print(x-k*d)
else:
    print(min(x%d,abs(d-x%d)))
x, k, d = map(int, input().split())

if x > 0:
    sgn_x = 1
    x_greedy = x - k * d
else:
    sgn_x = -1
    x_greedy = x + k * d

if x_greedy > 0:
    sgn_x_greedy = 1
else:
    sgn_x_greedy = -1

if sgn_x == sgn_x_greedy:
    print(abs(x_greedy))
    exit()

x_r = x % d
x_l = -(x_r - d)
print(min(x_r, x_l))
X, K, D = map(int, input().split())

k = K - (X // D)
x = X % D
while True:
    if abs(x) < D:
        if k % 2 == 0:
            print(abs(x))
        else:
            print(D-abs(x))
        exit()

    x = abs(x - D)
    k -= 1
    if k == 0:
        print(x)
        exit()
x,k,d = map(int,input().split())
a = x

if x >= k*d or x <= -k*d:
    print(str(abs(x - k * d )))
else:
    for i in range(k):
        if abs(a + d) > abs(a - d):
            a = a-d
        else:
            a = a+d
    print(str(abs(a)))
x, k, d = map(int, input().split())

div = x // d
if k <= div:
    ans = x - k * d
else:
    if (k - div) % 2 == 1:
        ans = x - d * (div + 1)
    else:
        ans = x - d * div

print(abs(ans))
X, K, D = map(int, input().split(" "))
X = abs(X)

a = X // D
b = X % D

remain_count = K - a

if remain_count < 0:
    ans = X - K * D
else:
    if remain_count % 2 == 0:
        ans = min(b, abs(b - D))
    else:
        ans = abs(b - D)
    
print(ans)
X,K,D = map(int, input().split())
place = X

if K > 50000 and K % 2 == 0:
    K = 10000
if K > 50000 and K % 2 == 1:
    K = 10001

for i in range(K):
    if place == 0 and (K-i)%2==0:
        break

    if place >= 0 and (place - D) >= 0:
        place -= D

    elif place >= 0 and (place - D) <= 0:
        place -= D
  
    else: 
        place += D 
      
if place < 0:
    place = abs(place)
print(place)
x,k,d=map(int,input().split())
R=[]
for i in range(1,k+1):
    res=abs(x-i*d)
    R.append(res)
print(min(R))
inp = list(map(int,input().split(" ")))

location = inp[0]
times = inp[1]
distance = inp[2]

x = float(abs(location / distance))

if x >= times:
    print(abs(location - (times*distance)))
elif (times - int(x))%2 == 0:
    print(abs(location - abs(int(x)*distance)))
else:
    print(abs(location - abs(int(x)*distance)-distance))
import sys
input = sys.stdin.readline


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


def main():
    X, K, D = readnums()
    if X // D >= K:
        print(X - K * D)
    else:
        a1 = X % D
        a2 = D - X % D
        if (K - X // D) % 2:
            print(a2)
        else:
            print(a1)


if __name__ == "__main__":
    main()
X, K, D = map(int, input().split(" "))
K_s = str(K)
K2 = int(K_s[len(K_s) - 1])

if K2 % 2 == 1:
    if X < 0:
        ans = X + D
    else:
        ans = X - D
else:
        ans = X

print(ans)
x, k, d = map(int, input().split())

x = abs(x)
ans = x
y = x - k * d
n = x // d
if 0 <= y:
    ans = min(ans, y)
elif n % 2 == k % 2:
    ans = min(ans, x % d)
else:
    ans = min(ans, d - x % d)

print(ans)
x,k,d = map(int, input().split())
mx = x + d*k
mn = x - d*k

if (x < 0 and mx > 0) or (x > 0 and mn < 0):
  if k%2 == 0:
    b = abs(x%(2*d))
    c = abs(mn%(2*d))
    e = abs(mx%(2*d))
    b = min(b,c,e)
  else:
    b = abs(mn%(2*d))
    e = abs(mx%(2*d))
    b = min(b,e)
  ans = min(b,abs(mx),abs(mn))
else:
  ans = min(abs(mx),abs(mn))
print (ans)
X, K, D = map(int, input().split())

if K <= abs(X // D):
    print(abs(X) - D * K)
elif (K - abs(X // D)) % 2 == 0:
    print(abs(X) % D)
else:
    print(D - abs(X) % D)
X,K,D = map(int,input().split())
X = abs(X)
 
if X <= K * D :
  if K % 2 == 1 :
    answer = abs(X % D - D)
  else :
    answer = X % D
else :
  answer = X - K * D
  
print(answer)
X,K,D = map(int,input().split())
A = abs(X)
ans = 0
if A >= D*K:
    ans = A - (D * K)
    K = 0
else :
    ans = A - (A//D)*D
    K = K - (A//D)
    if K%2 == 1:
        ans = A - D
print(abs(ans))
x, k, d = map(int, input().split())

a = [abs(x // d), x % d]

if a[0] > k:
   ans = min(x+k*d ,x-k*d)
else:
    b = (k- a[0]) % 2
    ans = min(x+b*d ,x-b*d)

print(abs(ans))