#include <stdio.h>
int main(void) {
	int A, B, C, K;

	//cin >> A >> B >> C >> K;
	scanf("%d %d %d %d\n", &A, &B, &C, &K);
	int sum = 0;

	K = K - A - B;
	sum = A;
	if (K < 0) {
//		cout << A << endl;
		printf("%d\n", sum);
	//	return 0;
	}
	else {

		sum = sum - K;
		printf("%d\n", sum);
	}
	return 0;
}