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];
        K -= arr[1];    
        console.log(K>0 ? total-K:total)
    } else {
        total += K;
        console.log(total);
    }
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
