#include <stdio.h>
#include <string.h>
int main()
{
  long long int a,b,c,k;
  scanf("%lld %lld %lld %lld",&a,&b,&c,&k);
  if(a>=k)  printf("%lld",k);
  else if(a<k&&(a+b)>=k) printf("%lld",a);
  else if((a+b)<k) printf("%lld",a-(k-a-b));
  return 0;
}
