import java.util.Scanner

import scala.io.StdIn._
object Main extends App {

  val Array(a,b,c,k) = scala.io.StdIn.readLine().split(" ").map(_.toInt)

  def solve() = {
    k match {
      case x if x <= a => 1 * x
      case x if x >= a && x <= a+b => a
      case x if x >= a+b && x <= a+b+c => a - (x - a - b)
      case _ => a - c
    }
  }

  println(solve())

}

