const input = require("fs").readFileSync("/dev/stdin", "utf8")
const lines = input.split('\n')
const [N, K] = lines[0].split(' ').map(BigInt)
let ans = N % K
ans = ans < - (ans - K) ? ans : - (ans - K)
console.log(ans.toString())