#include <stdio.h>

int main(){
  int a,b,c,k;
  scanf("%d%d%d%d",&a,&b,&c,&k);
  
  int ans = 0;
  
  if(k <= a){
    ans = k;
    k = 0;
  }else{
    ans = a;
    k -= a;
  }
  
  k -= b;
  
  if(k>0){
    ans -= k;
  }
  
  printf("%d\n",ans);
}
