
function main(input) {
	input =  input.split(" ");
	const A = input[0];
	const B = input[1];
	const C = input[1];
	const K = input[3];
	
	if (A+B>=K){
	    console.log(A);
	} else {
	    console.log(B-Math.min(C,K));
	}

}


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