process.stdin.resume();
process.stdin.setEncoding('utf8');

const lines = [];
const reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line)
});
reader.on('close', () => {
  const inputArray = lines[0].split(' ');
  const inputArrayNumber = inputArray.map( str => parseInt(str, 10));
  let result = inputArrayNumber[0]
  const t = inputArrayNumber[0] % inputArrayNumber[1]
  if(t < Math.abs(inputArrayNumber[1] - t)){
    console.log(t)
  }else{
    console.log(Math.abs(inputArrayNumber[1] - t))
  }
});