fun main() {
    val l = readLine()!!.split(' ').map { it.toInt() }
    var k = l[3]
    var max = 0
    if (k < l[0]) {
        max = k
    } else {
        max = l[0]
        k -= l[0]
        if (k > l[1]) {
            k -= l[1]
            max -= k
        }
    }
    println(max)
}
