fun main(args: Array<String>){
    // a(1n) b(0b) c(-1n) k(calc)
    var (a, b, c ,k) = nextIntList()
    
    var num = 0
    
    for(i in 1..k){
        if(a > 0){
            num += 1
            a-=1
            continue
        }
        if(b > 0){
            b-=1
            continue
        }
        if(c > 0){
            num -=1
        }
    }
    println(num)



}
fun next() = readLine()!!
fun nextIntList() = next().split(" ").map{ it.toInt() }


