function Main(input) {
	input = input.split("\n");
	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
    if(Math.abs(temp - K) < temp) return console.log(Math.abs(temp - K))
  	console.log(temp)
}

function absoluteSub(N, K) {
  return Math.abs(N - K)
}

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