function Main(input){
    const arr = input.split(' ').map(Number);
    let K = arr[3];
 
    if(K>=arr[0]){
        K -= (arr[0]+arr[1]);
        console.log(K>0 ? arr[0]-K:arr[0])
    } else {
        console.log(arr[0]+K);
    }
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
