fun main(args: Array<String>) {
    val (a, b, c, k) = readLine()!!.split(" ").map(String::toInt)
    var life = k
    var result = 0

    for (i in 1..a) {
        if (life > 0) {
            result += 1
            life--
        } else {
            break
        }
    }

    for (i in 1..b) {
        if (life > 0) {
            result += 0
            life--
        } else {
            break
        }
    }

    for (i in 1..c) {
        if (life > 0) {
            result -= 1
            life--
        } else {
            break
        }
    }

    println(result)
}