import scala.io.StdIn._
object Main {
  def main(args:Array[String]) {
    var read = readLine().split(' ').map(_.toInt)

    if(read(3) <= read(0)) println(read(3))
    else if(read(3) <= (read(0) + read(1))) println(read(0))
    else println(2 * read(0) + read(1) - read(3))
  }
}
