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