#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
	int A,B,C,K;
	int i,j;
	char str[100000];
 
	fgets(str, sizeof(str), stdin);
	A=atoi(strtok(str," "));
	B=atoi(strtok(NULL," "));
	C=atoi(strtok(NULL," "));
	K=atoi(strtok(NULL," "));

	if(K<A){
		printf("%d\n",K);
	}else if( K < (A+B) ){
		printf("%d\n",A);
	}else{
		printf("%d\n",(2*A+B-K));
	}

	return 0;
}
