import java.util.Scanner


import scala.io.StdIn._

object Main extends App {

  val Array(n,k) = readLine().split(" ").map(_.toLong)

  def solve() = {
    val quotient = n/k
    val mod = n%k
    if(mod == 0) 0L
    else if (quotient == 0) mod
    else Math.min(quotient, mod)
  }

  println(solve())

}

