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

var lines = []
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});

reader.on('line', (line) => {
  lines.push(line);
});

reader.on('close', () => {
  var nk = lines[0].split(' ');
  var n =Number(nk[0]);
  var k =Number(nk[1]);

  
  var a = n % k;
  var b = Math.abs(k - a);
  if(a < b){
    console.log(a);
  }else if(a > b)
    console.log(b);
  }else{
    console.log("0");
  }
});