import Foundation

let input = readLine()!

let info = input.componentsSeparatedByString(" ")

let val = Int64(info[0])!
let divider = Int64(info[1])!
let mod1 = val % divider
let mod2 = abs(mod1 - divider)

print(min(mod1, mod2))