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