#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
  int a,b,c,k;
  cin >> a >> b >> c >> k;
  if (a>=k){
    cout << k << endl;
  }
  else if (a + b >= k) {
    cout << a << endl;
  }
  else if(a + b + c >= k){
    int n = k-(a+b);
    cout << a + (-1*n) << endl;
  }
}