import java.lang.Long.min
import java.lang.Math.abs


fun main (args:Array<String>) {
    var (N, K) = readLine()!!.split(" ").map(String::toLong)
    var minResult = N
    if (N > K) {
        N = N % K
        minResult = N
    }
    while (true) {
        N = abs(N - K)
        if (minResult == N) {
            break
        }
        minResult = min(N, minResult)
    }
    print(minResult)
}
