#include <stdio.h>
int main(void){
  long A,B,C,K,sum=0;
  scanf("%ld %ld %ld %ld", &A, &B, &C, &K);
  if(A>=K){
    printf("%ld\n", K);
  }else{
    if(A+B>=K){
      printf("%ld\n", A);
    }else{
      // A+B+C>=K
      printf("%ld\n", A-(K-A-B));
    }
  }
  return 0;
}