#include <stdio.h>
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))

int main(){
	int a,b,c,k;
	scanf("%d%d%d%d",&a,&b,&c,&k);
	printf("%d",1*min(a,k)+0*min(b,max(0,k-a))+(-1)*min(c,max(0,k-a-b)));
}