#include<stdio.h>

int main()
{
    int a, b, c, k, sum = 0, n, m;
    scanf("%d %d %d %d", &a, &b, &c, &k);
    if(a >= k){
        sum = k;
    }
    else{
        sum = a;
        n = k - a;
        if(b >= n){
            sum = sum ;
        }
        else{
            m = n - b;
            sum = sum - m;
        }
    }
    printf("%d", sum);

    return 0;
}