fun main(){
    val (a,b, c, k) = readLine()!!.split(" ").map(String::toInt)

    if(a >= k){
        print("$k")
    }
    else if(a < k && a+b >= k){
        print("$a")
    }
    else{
        val max = a+(k-a-b)*(-1)
        print(max.toString())
    }

}