// 🐋💭
import Foundation

func readInts() -> [Int] {
    return readLine()!.split(separator: " ").map { Int($0)! }
}

func main() {
    let nk = readInts()
    let (n, k) = (nk[0], nk[1])
    //__________________________________ここまで入力処理
    let surplus = n % k
    
    print(min(surplus, abs(surplus - k)))
}


main()


