#include<bits/stdc++.h>
#pragma GCC optimize ("-O3")
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define pb push_back
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define diff(a,b) (a>b?a-b:b-a)
#define fr(n) for(int i=0;i<n;i++)
#define forn(i,n) for(int i=0;i<n;i++)
const int N=1e6+5;
const int MAX=1e7+9;
int binarySearch(int arr[], int l, int r, int x) 
{ 
    if (r >= l) { 
        int mid = l + (r - l) / 2; 
  
        // If the element is present at the middle 
        // itself 
        if (arr[mid] == x) 
            return mid; 
  
        // If element is smaller than mid, then 
        // it can only be present in left subarray 
        if (arr[mid] > x) 
            return binarySearch(arr, l, mid - 1, x); 
  
        // Else the element can only be present 
        // in right subarray 
        return binarySearch(arr, mid + 1, r, x); 
    } 
  
    // We reach here when element is not 
    // present in array 
    return -1; 
} 
void pairsort(int a[], int b[], int n){
    pair<int, int> pairt[n];
    for (int i = 0; i < n; i++) 
    {
        pairt[i].first = a[i];
        pairt[i].second = b[i];
    }
    sort(pairt, pairt + n);
    for (int i = 0; i < n; i++) 
    {
        a[i] = pairt[i].first;
        b[i] = pairt[i].second;
    }
}
int gcd(int a, int b){
    if (b == 0)
        return a;
    return gcd(b, a % b); 
}
int isprime(int n){
    if(n < 2)
        return 0;
    if(n < 4)
        return 1;
    if(n % 2 == 0 or n % 3 == 0)
        return 0;
    for(int i = 5; i*i <= n; i += 6)
        if(n % i == 0 or n % (i+2) == 0)
            return 0;
    return 1;
}
long long C(int n, int r) {
    if(r>n-r) 
        r=n-r;
    long long ans=1;
    for(int i=1;i<=r;i++){
        ans*=n-r+i;
        ans/=i;
    }
    return ans;
}
int mod = 1e9+7;
int modexpo(int x,int p){
    int res = 1;
    x = x%mod;
    while(p){
        if(p%2)
            res = res * x;
        p >>= 1;
        x = x*x % mod;
        res %= mod;
    }      
    return res;
}
int countDistinct(int arr[], int n) 
{ 
    // First sort the array so that all 
    // occurrences become consecutive 
    sort(arr, arr + n); 
  
    // Traverse the sorted array 
    int res = 0; 
    for (int i = 0; i < n; i++) { 
  
        // Move the index ahead while 
        // there are duplicates 
        while (i < n - 1 && arr[i] == arr[i + 1]) 
            i++; 
  
        res++; 
    } 
  
    return res; 
}

int int2Bin(string s){
    reverse(s.begin(),s.end());
    int b=1;
    int x=0;
    for (int i = 0; i < s.length(); ++i){
        if(s[i]=='1')
            x+=b;
        b*=2;;
    }
    return x;
}

int getSum(int n) 
 { 
    int sum; 
  
    /* Single line that calculates sum */
    for (sum = 0; n > 0; sum += n % 10, n /= 10); 
  
    return sum; 
 }


string intToString(int n){
    string s ="";
    while(n>0){
        s+=char(n%10+'0');
        n/=10;
    }
    reverse(s.begin(), s.end());
    return s;
}

int lcm(int n,int m){
    return (n*m)/__gcd(n,m);
}



int32_t main()
{
int q;
//cin>>q;
q = 1;
while(q--)
{
int a,b,c,k;
cin>>a>>b>>c>>k;
int sum = 0;

    while(a>0 && k>0)
    {
        a--;
        k--;
        sum+=1;
    }
    while(b>0 && k>0)
    {
        b--;
        k--;
    }

    while(c>0 && k>0)
    {
        c--;
        k--;
        sum-=1;
    }
    cout<<sum;
}
}







Measured memory_bytes: 3604