object Main extends App {
  @scala.annotation.tailrec
  def loop(n: Long, k: Long): Long = {
    val abs = math.abs(n - k)
    if (n % k == 0) 0 else if (n <= abs) n else loop(abs, k)
  }
  val sc = new java.util.Scanner(System.in)
  println(loop(sc.nextLong(), sc.nextLong()))
}
