#include <stdio.h>

int main(int argc, char const *argv[])
{
    unsigned long long A, B, C, K, left_cards;
    scanf("%llu %llu %llu %llu", &A, &B, &C, &K);
    left_cards = K;
    if (left_cards <= A)
    {
        printf("%llu\n", K);
        return 0;
    }

    left_cards -= A;
    if (left_cards <= B)
    {
        printf("%llu\n", A);
        return 0;
    }

    left_cards -= B;
    printf("%llu\n", A - left_cards);
    return 0;
}
