#include <stdio.h>

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

  	scanf("%d %d %d %d", &a, &b, &c, &k);
  	if (a + b + c > k ) {
	  zan = k;
    } else {
      zan = a + b + c;
    }
	if (a < k) {
		ret = a;
    } else {
      	ret = k;
    }
  	zan -= a;
  
  	if (zan > b) {
      zan -= b;
      if (zan > 0) {
        ret -= zan;
      }
    }
  
  	printf("%d\n", ret);
  	return 0;
}