function main(input) {
  var nums = input.split(' ');

  var N = parseFloat(nums[0], 10);
  var K = parseFloat(nums[1], 10);

  if (K === 1) {
    console.log('0');
    return;
  }

  if (N > K) {
    N = N % K;
  }

  var x = N;
  var memo = [x];
  var res = x;

  while (true) {
    x = Math.abs(x - K);
    if (memo.indexOf(x) != -1) break;
    if (x < res) {
      res = x;
    }
    memo.push(x);
  }

  console.log(String(res));
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
