#include<stdio.h>
#include<string.h>

int main()
{
    int a, b, c, k;
    int max_total = 0;

    scanf("%d %d %d %d", &a, &b, &c, &k);

    if (a >= k)
    {
        max_total = k;
    } else if ((a+b) >= k){
        max_total = a;
    } else {
        max_total = a + -1*(k - (a+b));
    }

    printf("%d", max_total);

    return 0;
}