#include <stdio.h>
#include <string.h>
#include <math.h>
#pragma warning(disable : 4996)

int main()
{
    long A, B, C, K;
    
    int i, j;
    int r;
    long max;

    scanf("%ld %ld %ld %ld", &A, &B, &C, &K);

    if (A + B >= K)
        if (A >= K)
            max = K;
        else
            max = A;
    else
        max = A - (K - A - B) * 1;

    printf("%ld", max);

    return 0;

}