#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <math.h>
#ifdef __cplusplus
#include <bits/stdc++.h>
#endif

uint32_t nextpint(void){ char c=getchar_unlocked(); while(c<'0'||'9'<c) c=getchar_unlocked(); uint32_t x=0; while('0'<=c && c<='9'){ x=x*10+c-'0'; c=getchar_unlocked(); } return x; }

int main(void){
	uint32_t a=nextpint();
	uint32_t b=nextpint();
	uint32_t c=nextpint();
	uint32_t k=nextpint();
	int32_t ans;
	if(a>=k){
		ans=k;
	}else if(a+b>=k){
		ans=a;
	}else{
		ans=a-(k-a-b);
	}
	printf("%"PRId32"\n", ans);
}
