function Main(input) {
	var str = input;
	var ans = 0;
	str = str.split("\n")[0];
	str = str.split(" ");
	var a = Number(str[0]);
	var b = Number(str[1]);
	var c = Number(str[2]);
	var k = Number(str[3]);
	ans = Number(ans);
	if (a <= k){
	ans = ans + a;
	k = k - a;
   	}else{
   		ans = a;
   	}
	k = k - b;
	if(k > 0){
	ans = ans - k;
	}
   console.log(ans);
}
 //*おまじない
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
