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