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