"use strict";
function main(input) {
  let kari = input[0].trim().split(" ").map(i => parseInt(i));
  let n = kari[0];
  let k = kari[1];

  if(n===k){
    console.log(0);
  }else{
    n = n % k;
    while(true){
      if(Math.abs(n-k) < n){
        n = Math.abs(n-k);
      }else{
        console.log(n);
        break;
      }
    }
  }

}
main(require('fs').readFileSync('/dev/stdin', 'utf8').trim().split("\n"));