
function Main(input) {
    input = input.trim();
    input = input.split(" ")

    var a = parseInt(input[0])
    var b = parseInt(input[1])
    var c = parseInt(input[2])
    var d = parseInt(input[3])

    if (d <= a) {
        console.log(d)
    } else {
        if (d <= a + b) {
            console.log(a)
        } else {
            console.log(a - (d - (a + b)))
        }
    }
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));
