function main(input) {
  const inputs = input.split('\n')
  const [N, K] = inputs[0].split(' ').map(BigInt)
  const r = N % K
  if (K < 2n * r) {
    console.log(String(K - r))
  } else {
    console.log(String(r))
  }
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));

