#include<stdio.h>




int main()
{
	int a, b, c, k,sum=0;

	scanf("%d %d %d %d", &a, &b, &c, &k);
	
	if ((a + b) >= k)
	{
		sum = a;
	}
	else
	{
		k = k - (a + b);
		sum = (a-k);
	}
	printf("%d", sum);

	return 0;
}