#include <stdio.h>

int main()
{
    long long int a, b, c, k, sum, mcard;
    scanf("%lld %lld %lld %lld", &a, &b, &c, &k);
    if (k<=a) printf("%lld\n", a);
    else {
        sum=a+b;
        mcard=k-sum;
        if (mcard<=0) printf("%lld\n", a);
        else printf("%lld\n", a-mcard);
    }
    return 0;
}
