import java.util.Scanner

object Main {
  def main(args: Array[String]) = {
    val s = new Scanner(System.in)
    val n = s.nextLong
    val k = s.nextLong
    val mod = n % k
    print(scala.math.min(mod, (mod - k).abs.toLong))
  }
}