#include <stdio.h>

 
 
int main()
 {
	int a,b,c,k;

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

	int count=0;

	
			if(a>=k){
				count=k;
				
			}

		else{
			count=a;
			k=k-a-b;
			
			if(k>0){
				for(int i=0;i<k;i++){
					count--;
				}
			}
		}
		

printf("%d",count);
	return 0;
}