import scala.io.Source

object Main extends App {
  val inputStrings = Source.stdin.getLines().toList
  val arr = inputStrings.head.split(' ').take(2).map(_.toLong)
  val result = arr match {
    case Array(n, k) if n == 0 | k == 1 => 0
    case Array(n, k) =>
      val abs = (n - k).abs
      mod(abs, k)
  }
  
  print(result)
  
  def mod(a: Long, b: Long): Long = {
    if (a >= b)
      a % b
    else
      b % a
  }
}
