import Foundation

let inputLine = readLine()!.components(separatedBy: " ").map({Int($0)!})
let N = inputLine[0]
let K = inputLine[1]

var ans = N

func swapAbs() {
    if abs(ans-K)>ans{
        return
    }
    ans = abs(ans-K)
    swapAbs()
}

let div = N/K
ans = N-K*div
swapAbs()
print(ans)
