#include<stdio.h>

int main(void){
int A,B,C,K,point,L,M,N;
scanf("%d",&A);
scanf("%d",&B);
scanf("%d",&C);
scanf("%d",&K);
L = K - A; 
if (A < K){
for (int i = 0, i <= A; ++i);
    point = 1 + point;
else
for (int j = 0, j <= L; ++j);
    point = 1 + point;}
M = K - A - B;
for (int k = 0, k <= M; ++k);
    point = -1 + point;
printf("%d",point);

return 0;
}