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

 if(n % k == 0){
   console.log("0");
 }else{

 while(true){
   var a = Math.abs(n-k);
   n = a;
  
   if(ans.indexOf(a) !== -1){
     break;
   }
   ans.push(a);
   }
 
var min = ans.sort((a, b)=> a - b)[0];
console.log(min);
}
});