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]);

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