#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<vector<int>> Matrix;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
static const int INF = 2000000000;


int main(){
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  ll ans = a;
  if (k > a + b) ans -= k-a-b;
  cout << ans << endl;
} 