#include<stdio.h>
int main(){
    double a,b,c,k;
    double sum=0;
    scanf("%lf %lf %lf %lf",&a,&b,&c,&k);
    if(a<=k){
        sum=sum+a*1;
        k=k-a;
    }else if(k<a){
        sum=k*1;
        printf("%.0f\n",sum);
        return 0;
    }
    if(b<=k){
        sum=sum+0;
        k=k-b;
    }else if((k<b)&&(k!=0)){
        sum=sum+0;
        printf("%.0f\n",sum);
        return 0;
    }
    if(c<=k){
        sum=sum+c*-1;
        k=k-c;
    }else if((k<c)&&(k!=0)){
        sum=sum+k*-1;
        printf("%.0f\n",sum);
        return 0;
    }
    printf("%.0f\n",sum);
}