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