function Main(input) {
	tmp = input[0].split(" ");
	var N = Number(tmp[0])
    var K = Number(tmp[1])
    if(K === 1) return console.log(0)
  	if(N < K) return console.log(N)
  	var temp = N % K
    var current = Math.abs(temp - K)
    if(current <= temp) return console.log(current)
  	console.log(temp)
}

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