#include <stdio.h>
#include <string.h>

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

  return 0;
}