import java.io.PrintWriter
import java.util.StringTokenizer
import kotlin.math.abs
import kotlin.math.min

fun main() {
    val reader = System.`in`.bufferedReader()
    val writer = PrintWriter(System.out, false)

    var tokenizer: StringTokenizer = StringTokenizer("")

    fun next(): String {
        while (tokenizer.hasMoreTokens().not()) tokenizer = StringTokenizer(reader.readLine())
        return tokenizer.nextToken()
    }

    fun nextInt() = next().toInt()
    fun nextLong() = next().toLong()
    fun nextDouble() = next().toDouble()
    fun nextLine() = reader.readLine()

    fun PrintWriter.solve() {
        var (n,k) = List(2){nextLong()}
        n%=k

        print(min(n, abs(n-k)))


    }

    writer.solve()
    writer.flush()


}


