import scala.util.control.Breaks._

object Main extends App {
  var Array(n, k) = scala.io.StdIn.readLine.split(" ").map(_.toLong)

  breakable {
    while (true) {
      if (n > k) {
        n = k - 1
      } else {
        val temp = scala.math.abs(n - k)
        if (temp < n) {
          n = temp
        } else {
          println(n)
          break()
        }
      }
    }
  }
}
