#include<bits/stdc++.h>
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define max4(a,b,c,d) max(max(a,b),max(c,d))
#define min4(a,b,c,d) min(min(a,b),min(c,d))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/__gcd(a,b)
#define print1(a,n) for(int i=1;i<=n;i++) cout<<a[i]<<" "
#define print21(a,n,m) for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cout<<a[i][j]<<" ";}cout<<endl;}
#define scan1(a,n) for(int i=1;i<=n;i++) cin>>a[i]
#define scan(a,n) for(int i=0;i<n;i++) cin>>a[i]
#define print(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "
#define int long long
#define double float
#define float long double
#define vi vector<int>
#define si set<int>
#define vb vector<bool>
#define vc vector<char>
#define pii pair<int,int>
#define piii pair<int,pair<int,int>>
#define mkp make_pair
#define mkt make_tuple
#define mod 1000000007
#define infinite 1e9
#define ff first
#define ss second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define mset(m,v) memset(m,v,sizeof(m))
#define bs binary_search
#define cout1(a) cout<<a<<endl
#define cout2(a,b) cout<<a<<" "<<b<<endl
#define cout3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
#define cout4(a,b,c,d) cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl
#define cin2(a,b) cin>>a>>b
#define cin3(a,b,c) cin>>a>>b>>c
#define cin4(a,b,c,d) cin>>a>>b>>c>>d
#define endl '\n'
#define fr(i,a,b) int i; for(i=a;i<=b;i++)
using namespace std;
bool comp(int x,int y){
	if(x>=y)
	return true;
	return false;
} 
bool compare(const pii &i,const pii &j){
	return i.ff<=j.ff;
}
bool compare1(const pii &i,const pii &j){
	return i.ff>j.ff;
}
string decitobinary(int n,int bit){
    char c[bit];    // 32 bit binary number
    memset(c,'0',sizeof(c));
    int i=0; 
    while (n>0){ 
        c[i]=n%2+48; 
        n=n/2; 
        i++; 
    }
    string s="";
    for(int j=bit-1;j>=0;j--)   // 32 bit binary number
        s+=c[j];
    return s;
}
int binarytodeci(string n) 
{ 
    string num=n; 
    int dec_value=0; 
    int base=1; 
    int len=num.length(); 
    for(int i=len-1;i>=0;i--){ 
        if(num[i]=='1') 
            dec_value+=base; 
        base=base*2; 
    }
    return dec_value; 
} 
double intlog(double base,double x){
    return (log(x)/log(base));
}
int fact(int n){
    int res=1; 
    for(int i=2;i<=n;i++) 
    res=res*i; 
    return res; 
}
int ncr(int n,int k)  
{  
    int res=1;  
    if(k>n-k)  
    k=n-k;  
    for(int i=0;i<k;++i){
        res*=(n-i);  
        res/=(i+1);  
    }  
    return res;  
}
bool ispalindrome(string str,int l,int h){
    while(h>l){
        if (str[l++]!=str[h--]) 
        return 0;
    } 
    return 1;
} 
int compute_hash(string s){
	int p=31,hash=0,po=1;
	for(char c:s){
		hash+=(c-'a'+1)*po%mod;
		po=(po*p)%mod;
	}
	return hash;
}
vector<vector<int>> group_identical_strings(vector<string> const& s) {
    int n = s.size();
    vector<pair<long long, int>> hashes(n);
    for (int i = 0; i < n; i++)
        hashes[i] = {compute_hash(s[i]), i};
    sort(hashes.begin(), hashes.end());
    vector<vector<int>> groups;
    for (int i = 0; i < n; i++) {
        if (i == 0 || hashes[i].first != hashes[i-1].first)
        groups.emplace_back();
        groups.back().push_back(hashes[i].second);
    }
    return groups;
}
bool isarraysort(int a[],int i,int j){
    if(j==i)
    return 1;
    if(a[j]<a[j-1])
    return 0;
    return isarraysort(a,i,j-1);
}
void sieve(int n,int prime[]){ // till 1e5 nloglogn
    for(int i=1;i<=n;i++)
    prime[i]=1;                // int prime[n+1]
    for(int p=2;p*p<=n;p++){ 
        if(prime[p]==1){  
            for(int i=p*p;i<=n;i+=p) 
                prime[i]=0; 
        } 
    } 
}
bool isprime(int n){ 
    for(int i=2;i*i<=n;i++){
        if(n%i==0)
        return 0;
    }
    return 1;
}
int pow_mod(int base,int exp) {
    int res=1;
    while(exp>0){
    if(exp%2==1)
    res=(res*base)%mod;
    exp=exp>>1;
    base=(base*base)%mod;
    }
    return res;
}
const int N=1e6+3;
vector<vector<int> > adj;
bool visited[N];
int depth[N]={0};
int size[N]={0};
int parent[N];
void dfs(int v){
    visited[v]=true;
    for(int u:adj[v]){
        if(!visited[u]){
            dfs(u);
        }
    }
}
int time_in[N],time_out[N];
int dfs_timer=0;
int color[N];
void dfs_time(int v){
    time_in[v]=++dfs_timer;
    color[v]=1;
    for(int u:adj[v]){
        if(color[u]==0){
            dfs_time(u);
        }
    }
    color[v]=2;
    time_out[v]=++dfs_timer;
}
int count1[N];
void numberOfNodes(int s)
{
    count1[s] = 1;
    for (int u:adj[s]){
        if(u==parent[s])
        continue;
        numberOfNodes(u);
        count1[s]+=count1[u];
    }
}
void solve(){
    int a,b,c,k;
    cin4(a,b,c,k);
    if(k<=a)
    cout<<k;
    else if(k<=a+b)
    cout<<a;
    else if(k<=a+b+c)
    cout<<a*2-k+b;
    else
    cout<<a-c;
}
int32_t main(void){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	int t=1;
// 	cin>>t;
	while(t--){
		solve();
   }
}


Measured memory_bytes: 3684