fun main(args: Array<String>){
    var (A, B, C, K) = readLine()!!.split(" ").map { it.toInt() }
    var ans = 0

    for(i in 0 until K){
        if(A > 0) {
            ans += 1
            A --
        }else if(B > 0){
            ans+= 0
            B--
        }else{
            ans -= 1
            C --
        }
    }

    println(ans)

}
