let input = readLine()!
let splitInput = input.split(separator: " ")
var n = Int(splitInput[0])!
var k = Int(splitInput[1])!

var rest = n % k

if rest == 0 {
    print(0)
} else {
    rest -= k
    if rest < 0 {
        rest *= -1
    }

    if n < rest {
        rest = n
    }
    print(rest)
}
