#include<stdio.h>
int main(){
    int a,b,c,k;
    scanf("%d %d %d %d",&a,&b,&c,&k);
    if(a>=k){
        printf("%d\n",k);
    }else if(a+b>=k){
        printf("%d\n",a);
    }else{
        int t=k-a-b;
        printf("%d",a-1*t);
    }
    return 0;
}