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

    var n = 0L
    if (a > k) {
        println(k)
        return
    } else {
        n += a
        k -= a
    }

    if (b > k) {
        println(n)
        return
    } else {
        k -= b
    }

    n -= k

    println(n)
}



