#include <bits/stdc++.h>
using namespace std;

int main(){
 int A, B, C, K;
  cin >> A >> B >> C >> K;
  if (A >= K){
    cout << K << endl;
  }
  if (A < K <= A + B){
    cout << A << endl;
  }
  if (A + B < K){
    cout << A - C << endl;
  }
}