const main = (inputs) => {
  var array = inputs[0].split(" ");
  var N = array[0] - 0;
  var k = array[1] - 0;
  var minValue = N;
  var minus = Math.abs(N % k - k);

  if (N % k === 0) {
    minValue = 0;
  } else {
    minValue = minValue < minus ? minValue : minus;
    minValue = minValue < N % k ? minValue : N % k;
  }

  console.log(minValue);
}
main(require('fs').readFileSync('/dev/stdin', 'UTF-8').trim().split("\n"));