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