#include <stdio.h>

int main(){
	int a,b,c,k;
	int x,y,z,tmp,sum=0;

	scanf("%d %d %d %d",&a,&b,&c,&k);
	
	if(k-a >=0){
		x = k - a;
		sum = sum + a;
//printf("x=%d sum =%d \n",x,sum);
	} 
	
	if(x-b >=0){
		y = x - b;
//printf("y=%d sum =%d \n",y,sum);
		
	}
	
	if(y-c >=0){
		z = z - c;
		sum = sum - c;
//printf("z=%d sum =%d \n",z,sum);

	}
	else sum = sum - y;
	
		
	
	printf("%d",sum);

	return 0;
}
