//MD. HASNAIN ALI (UNSOLVED)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define MAX(a,b) (a>b? a:b)
#define MIN(a,b) (a<b? a:b)
#define SWAP(a,b) {       \
                    a=a+b;\
                    b=a-b;\
                    a=a-b;}
#define LL long long


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

    return 0;
}
