fun main(){
    var (N, K) = readLine()!!.split(" ").map(String::toLong)
    N = N % K
    while(true){
        if(K == 1L) {println("0"); return}
        if(N <= java.lang.Math.abs(N-K)) {println(N); return}
        else {N = java.lang.Math.abs(N-K)}
    }
}