function Main(input){
    const arr = input.split(' ').map(Number);
    let K = arr[3];
    let total=0;

    if(K>=arr[0]){
        total += arr[0];
        K -= arr[0];
    } else {
        total += K;
        console.log(total);
        return;
    }
    
    if(K>=arr[1]){
        K -= arr[1];
    } else {
        console.log(total);
        return;
    }

    if(K>=arr[2]){
        total -= arr[2];
        K -= arr[2];
    } else {
        total -= K;
        console.log(total);
        return;
    }
    console.log(total);
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));