#include <iostream>
#include <string>

using namespace std;

typedef long long ll;

int main()
{
    int a, b, c, k;
    cin << a << b << c << k;
    if (k <= a)
    {
        cout << k << endl;
    }
    else if (k <= a + b)
    {
        cout << a << endl;
    }
    else{
        cout << a - c << endl;
    }
    
    return 0;
}