function Main(arg) {
    var arg = arg.trim().split(' ');
    var n = Number(arg[0]);
    const k = Number(arg[1]);
    if (k == 1) {
        console.log(0);
        return
    }
    if(n > k){
        n = Math.abs(n % k)
    }
    while (n > Math.abs(n - k)) {
        n = Math.abs(n - k)
    }
    console.log(n);

}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));