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

    var a = input[0]
    var b = input[1]
    var c = input[2]
    var d = 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"));
