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