#include <iostream>
using namespace std;

int main () {
    int A, B, C, K;
    int sum == 0;

    cin >> A >> B >> C >> K;

    if (K <= A) {
        sum = K;
    } else if (K <= A + B) {
        sum = A;
    } else {
        sum = A - C;
    }

    cout << sum;

    return 0;
}