#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define be begin()
#define en end()
#define le length()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repo(i, n) for (ll i = 1; i <= n; i++)
#define foreach(c, itr) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
#define fastIO                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define eps 1e-6
#define pi 3.141592653589793
#define c(a) cout << a << "\n";
#define ao(a)        \
    for (auto i : a) \
        cout << i << " ";
#define endl '\n'
typedef vector<int> vii;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int N = 1e6 + 6;

ll powmod(ll a, ll b, ll mod)
{
    ll res = 1;
    a %= mod;
    for (; b; b >>= 1)
    {
        if (b & 1)
            res = (res * a) % mod;
        a = (a * a) % mod;
    }
    return res;
}

ll gcd(ll a, ll b)
{
    while (b > 0)
    {
        ll t = a % b;
        a = b, b = t;
    }
    return a;
}

ll lcm(ll a, ll b)
{
    return (a / gcd(a, b)) * b;
}

ll is_prime(ll n)
{
    if (n <= 1 || n > 3 && (n % 2 == 0 || n % 3 == 0))
        return 0;
    for (ll i = 5, t = 2; i * i <= n; i += t, t = 6 - t)
        if (n % i == 0)
            return 0;
    return 1;
}
ll modI(ll a, ll m)
{
    ll m0 = m, y = 0, x = 1;
    if (m == 1)
        return 0;
    while (a > 1)
    {
        ll q = a / m;
        ll t = m;
        m = a % m;
        a = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0)
        x += m0;
    return x;
}
ll powbin(ll base, ll p)
{
    ll mul;

    if (p == 1)
        mul = base;
    else
    {
        mul = (ll)powbin(base, p / 2);
        mul = (mul * mul) % MOD;
        if (p % 2)
            mul *= base;
    }

    return mul % MOD;
}

ll combination(ll n, ll r)
{
    ll num = 1, den = 1;

    for (ll i = n; i >= n - r + 1; i--)
    {
        num = (num * i) % MOD;
    }
    for (ll i = r; i >= 1; i--)
    {
        den = (den * i) % MOD;
    }
    return (num * powbin(den, MOD - 2)) % MOD;
}

// int v[(1<<20)];

// void init(void){
//     int i;
//     rep(i,(1<<20)) v[i] = 0;
// }

// void add(int x, int val){
//     for(int i=x;i<(1<<20);i=((i)|(i+1))) v[i] += val;
// }

// int sum(int x){
//     ll ans = 0;
//     for(int i=x;i>0;i=((i)&(i-1))) ans += v[i-1];
//     return ans;
// }
// long long get(vector<long long> &f, int pos) {
//     long long res = 0;
//     for (; pos >= 0; pos = (pos & (pos + 1)) - 1)
//         res += f[pos];
//     return res;
// }

// void upd(vector<long long> &f, int pos, int val) {
//     for (; pos < int(f.size()); pos |= pos + 1) {
//         f[pos] += val;
//     }
// }
/*************************************************************************************************************/
// int tree[N],a[N];
// ll build(ll l,ll r,ll node){
//     if(r<l) return N;
//     if(l==r) {tree[node]=a[l];return N;}
//     ll mid=(l+r)/2;
//     build(l,mid,2*node+1);
//     build(mid+1,r,2*node+2);
//     tree[node] = min(tree[2*node+1],tree[2*node+2]);

// }
// void update(ll l,ll r,ll node,ll val,ll pos){
//     if(r<l) return;
//     if(l==r && l==pos) {tree[node]=val;return;}
//     ll mid=(l+r)/2;
//     if(pos<=mid) update(l,mid,2*node+1,val,pos);
//     else update(mid+1,r,2*node+2,val,pos);
//     tree[node]=min(tree[2*node+1],tree[2*node+2]);
// }
// ll sum(ll l,ll r,ll x,ll y,ll node){
//     //if(x<l || y>r) return 0;
//     if(r<x || y<l) return N;
//     //if(l==r) return tree[node];
//    // if(y<x || r<l) return 0;
//     if(x<=l && r<=y) return tree[node];
//     ll mid=(l+r)/2;
//     return min(sum(l,mid,x,y,2*node+1),sum(mid+1,r,x,y,2*node+2));
// }
// bool prime[100005];
// void SieveOfEratosthenes(int n)
// {

//     memset(prime, true, sizeof(prime));

//     for (int p=2; p*p<100005; p++)
//     {

//         if (prime[p] == true)
//         {

//             for (int i=p*p; i<100005; i += p)
//                 prime[i] = false;
//         }
//     }
// }
// void compress()
// {
// 	map<int, int> comp;
// 	for(int i=1;i<=n;i++)
// 		comp[a[i]];
// 	int ctr = 0;
// 	for(auto &it:comp)
// 		it.second = ++ctr;
// 	for(int i=1;i<=n;i++)
// 		a[i] = comp[a[i]];
// }
int main()
{

    ll a, b, c, k;
    cin >> a >> b >> c >> k;
    if ((a + b) >= k)
        c(min(a, k)) else c((a) - (k - (a + b)))

            return 0;
}

Measured memory_bytes: 3648