"use strict";

const main = arg => {
    const input = arg.trim().split("\n");

    const N = BigInt(input[0].split(" ")[0]);
    const K = BigInt(input[0].split(" ")[1]);
    if(N%K == 0){
        console.log(0)
        return
    } 

    let tmp = N % K
    if(Number(tmp) < Math.abs(Number(K-tmp))) console.log(Number(tmp))
    else console.log(Math.abs(Number(K-tmp)))

}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
