#include <stdio.h>
#include<string.h>
int main()
{
    long int a,b,c,k;
    scanf("%ld %ld %ld %ld",&a,&b,&c,&k);
    if(k<a)
    printf("%ld",k);
    if(k>=a&k<=a+b)
    printf("%ld",a);
    if(k<=a+b+c&k>a+b)
    printf("%ld",a-(k-a-b));
}
