#include<stdio.h>

int main(void){

	int a, b, c, k, ans;

	scanf("%d %d %d %d", &a, &b, &c, &k);

	if(a >= k){
		ans = k;
	}
	else if(a < k && a + b >= k){
		ans = a;
	}
	else
		ans = a - (k - a - b);

	printf("%d", ans);

	return 0;
}