#include<stdio.h>

int main(void){
  int a, b, c, k;
  int result = 0;
  scanf("%d%d%d%d", &a, &b, &c, &k);
  if(a >= k){
    result = k;
  }else if((a+b) >= k){
    result = a;
  }else{
    result = a - (k - (a+b));
  }
  printf("%d\n", result);
  return 0;
}
