var fs = require("fs");
var buffer = fs.readFileSync(0); // STDIN_FILENO = 0
var inputs = buffer.toString().split(' ').map(v => parseInt(v));

function solve(N, K){
  var x = N % K;
  var kHalf = K / 2;
  while(x > kHalf){
    x = Math.abs(x - K)
  }
  console.log(x)
}

solve(inputs[0], inputs[1]);