// Problem : B - Easy Linear Programming
// Contest : AtCoder - AtCoder Beginner Contest 167
// URL : https://atcoder.jp/contests/abc167/tasks/abc167_b
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

//S->----------------------------------------------------|
//R->--------------</AUTHOR - SRIJAN DE>-----------------|
//I->----</JALPAIGURI GOVERNMENT ENGINEERING COLLEGE>----|
//J->------------</INFORMATION TECHNOLOGY>---------------|
//A->-------------</sd2220@it.jgec.ac.in>----------------|
//N->----------------------------------------------------|

#include    <bits/stdc++.h>
#include    <ext/pb_ds/assoc_container.hpp> // Policy_based_Data_Structure 
#include    <ext/pb_ds/tree_policy.hpp>
using       namespace            std;
using       namespace            __gnu_pbds;

/*#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;*/

using       ll=                  int64_t ;
using       ull=                 unsigned long long ;
using       ld=                  long double;
#define     int                  int64_t
#define     mem(a,val)           memset( a,  val,  sizeof(a) )
#define     b_s                  binary_search
#define     u_b                  upper_bound
#define     l_b                  lower_bound
#define     nl                   cout<<endl
#define     pb                   push_back
#define     pf                   push_front
#define     ppb                  pop_back
#define     ppf                  pop_front
#define     in                   insert
#define     fi                   first
#define     se                   second
#define     pqueue               priority_queue
#define     rr                   return
typedef     tree<int, null_type,less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

const int   N=                   1e6+7;
const int   MOD=                 (int)1e9 + 7;
const int   MOD1=                (int)1e9 + 9;
const int   MOD2=                998244353;
const ld    pi=                  atan2(0, -1);
const ld    eps=                 1e-9;
int         tests=               1;

template<class x,class y>x max(x a,y b){return (a>b)?a:b;}
template<class x,class y>x min(x a,y b){return (a<b)?a:b;}
template<class x,class y>x log(x n,y r){return (n>r-1)?1+log(n/r,r):0;}

vector<int> adj[N];
int vis[N]={0};

///.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.

int32_t main() 
{
	auto start = clock();
	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);	

//	cout<<fixed;
//	cout<<scientific;
//	cout<<fixed<<setprecision(12);
	// cin>>tests;
	for(int test=1;test<=tests;test++) 
	{
//		cout<<"Case #"<<test<<": ";
///.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.

		
		
		int a,b,c,k;
		cin>>a>>b>>c>>k;
		
		int ans=0;
		if(a>=k)
		{
		    cout<<k;
		    rr 0;
		}
		else
		{
		    ans+=a;
		    k-=a;
		}
		if(b>=k)
		{
		    cout<<ans;
		    rr 0;
		}
		else
		{
		    k-=b;
		}
		ans-=min(k,c);
		cout<<ans;
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
///.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.'.
		
	}
	cerr<<endl<<endl<<endl<< "Time : " << double(clock() - start) / CLOCKS_PER_SEC <<endl;
	return 0;
}



Measured memory_bytes: 27232