#include<stdio.h>
int main()
{
    int a, b, c, k, n1, n2, sum;
    scanf("%d %d %d %d", &a, &b, &c, &k);
    if(a>=k) sum=k;
    else{
        if(b>=k-a) sum=a;
        else{
            sum=a-(k-(a+b));
        }
    }
    printf("%d", sum);
    return 0;
}

