main :-
	read_num(X),
        read_num(K),
        read_num(D),

        AbsX = abs(X),

        find_location(AbsX, K, D, L),

        write(L),
        nl,
        halt.

find_location(X, K, D, L) :-    %　K回移動しても原点まで行けない場合
        X >= K * D,
        !,
        L is X - K * D.
find_location(X, K, D, L) :-    % K回までに原点を飛び越える場合
        FirstOverNumberOfTimes is ceiling(X / D),
        RemainNumberOfTimes is K - FirstOverNumberOfTimes,
        RemainNumberOfTimes mod 2 =:= 0, % -側
        !,
        L is abs(X - FirstOverNumberOfTimes * D).
find_location(X, K, D, L) :-    % +側
        OverOriginDistance is abs(X - ceiling(X / D) * D), % -側の点の絶対値
        L is abs(OverOriginDistance - D).                  % +側の点の絶対値


read_str(S) :-
        read_string(current_input, ' \n', '', _, S).

read_num(N) :-
	read_str(S),
	number_string(N, S).
