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