#include<stdio.h>

int main(void){
  int a, b, c, k, i, result = 0;

  scanf("%d%d%d%d", &a, &b, &c, &k);

  if(a <= k){
    result += a;
  }

  if((a + b) >= k){
    printf("%d", result);
  }else{
    result = result - (k - (a + b));
    printf("%d", result);
  }

  return 0;
}
