#include<stdio.h>

int main(void){
    int a, b, c, k;
    int max;
    
    scanf("%d%d%d%d", &a, &b, &c, &k);
    if(a >= k){
        max = k;
        k = 0;
    }else{
        k = k - a;
    }
    if(b >= k){
        printf("%d\n", max);
    }else{
        printf("%d\n", max-k);
    }

    return 0;
}