fun main() {
    val l = readLine()!!.split(" ").map { it.toInt() }
    if(l[3]<=l[0]){
        println(l[3])
    }else if(l[3]<=l[0]+l[1]){
        println(l[0])
    }else{
        println(l[0]-(l[3]-l[0]-l[1]))
    }
}