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

int main() {
	long int a, b, c, k,ans=0;
	scanf("%ld %ld %ld %ld", &a, &b, &c, &k);
	if (k <= a+b) {
		ans = a;
	}
	else {
		ans = a - (k - a - b);
	}
	printf("%ld", ans);
}