#include <stdio.h>

int main()
{
	int a, b, c, k, max = 0;
	scanf("%d %d %d %d", &a, &b, &c, &k);

	if(a < k){
		max += a;
		if(b < k - a){
			max = max - (k - a - b);
		}
	}else{
		max += k;
	}

	printf("%d", max);
}
