import java.io.PrintWriter

fun main(args: Array<String>) {
    // 入力
    val pw = PrintWriter(System.out)
    //入手
    val (a, b, c, d) = readLine()!!.split(" ")

    var e = d.toInt()
    var f = 0
    for (i in 1..a.toInt()) {
        f++
        e--
        if (e == 0) {
            pw.println(f)
            pw.flush()
            return
        }
    }
    e -= b.toInt()
    if (e <= 0) {
        pw.println(f)
        pw.flush()
        return
    }
    for (i in 1..c.toInt()) {
        f--
        e--
        if (e == 0) {
            pw.println(f)
            pw.flush()
            return
        }
    }
}