#include<bits/stdc++.h>
using namespace std;
int a,b,c,k,mx;
int main(){
	ios::sync_with_stdio(0);
	cin>>a>>b>>c>>k;
  	if(k<a)
      cout << a-k;
	if(k>a)
	{
		mx+=a;
		k-=a;
	}
	else
	{
		cout<<a<<endl;
		return 0;
	}
	if(k>b)
	{
		k-=b;
	}
	else
	{
		cout<<mx<<endl;
		return 0;
	}
	mx-=k;
	cout<<mx<<endl;
}