import java.util.Scanner

object Main extends App {
  val sc = new Scanner(System.in)
  val N, K = sc.nextLong

  val z =
    if (N % K == 0) 0
    else {
      var x = N % K
      var y = Set.empty[Long]
      while (!y.contains(x)) {
        y += x
        x = math.abs(x - K)
      }

      y.min
    }
  println(z)
}
