#include <stdio.h>
#include <inttypes.h>

int main(void) {
	int64_t A, B, C, K;
	int64_t answer = 0;
	if (scanf("%" SCNd64 "%" SCNd64 "%" SCNd64 "%" SCNd64, &A, &B, &C, &K) != 4) return 1;
	if (A >= K) {
		answer += K;
		K = 0;
	} else {
		answer = A;
		K -= A;
	}
	if (B >= K) {
		K = 0;
	} else {
		K -= B;
	}
	if (C >= K) {
		answer -= K;
	} else {
		answer -= C;
	}
	printf("%" PRId64 "\n", answer);
	return 0;
}
