#include <stdio.h>

int main(void)
{
  int a, b, c, k;
  int count = 0;
  
  scanf("%d%d%d%d", &a, &b, &c, &k);
  
  while (a > 0 && k > 0) {
      a--;
      k--;
      count++;
  }
  
  while (b > 0 && k > 0) {
      b--;
      k--;
  }
  
  while (c > 0 && k > 0) {
      c--;
      k--;
      count--;
  }
  
  printf("%d", count);
  return 0;
}
        