#include <stdio.h>

int main(void){
    int a, b, c, k, sum, a_num = 0, b_num = 0, c_num = 0;

    scanf("%d %d %d %d", &a, &b, &c, &k);
    for(int i = 0;i < k; i++){
        if(a_num != a){
            sum += 1;
            a_num++;
        }
        else if(b_num != b){
            b_num++;
        }
        else{
            sum -= 1;
            c_num++;
        }
    }

    printf("%d\n", sum);

    return 0;
}