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 array = [];

 var t = n % k;
 var next = t - k;

 array.push(t);
 array.push(next);

 var min = array.sort((a, b)=> a - b)[0];

 console.log(min);
 
});