#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
typedef long long ll;
#define showa(v) for(int iii = 0; iii < v.size(); iii++){ cout << v[iii] << " ";} cout << endl;
#define show(iiii) cout << iiii << endl; 
#define shows(iiiii) cout << iiiii << " ";
#include<iostream>
#include<string>
#include<vector>
#include<stdio.h>
#include <cmath>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <set>
#include <deque>
#include <numeric>
#include<tuple>
#include <utility>
#include <map>
#include <cassert>
using namespace std;
 
int main(){
    int a, b, c, k;
    cin >> a >> b >> c >> k;
    if (k <= a){
        cout << k << endl;
        return 0;
    }
    else if(k <= a+b){
        cout << a << endl;
        return 0;
    }
    else{
        cout << a - (k - (a+b)) << endl;
        return 0;
    }

 }