#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ll long long

int main(){
    ll a,b,c,k,ans=0;
    scanf("%lld%lld%lld%lld",&a,&b,&c,&k);
    if (k>a) ans+=a;
    else ans+=k;
    if (k>a+b) ans-=(k-a-b);
    printf("%lld",ans);
    return 0;
}


