/*******************************************************************************************************************************************************************************************************************************************************************
********************************************************************************************************************************************************************************************************************************************************************

                                                                                                                                                                                                                                                                `
                                                                                                                                                                                                                                                                   `
                                                                                                                                                                                                                                                                   `
                                                                                                                                               .:!|%|;'.                                                                                                           `
                                                                                                                                             ;&#&!'..|##&:                                                                                                         `
                                                                                                                  ...                      '$@@;  .`. `%@%:                                                                                                        `
                                                                                                                :%@#%`                    '&#%'.`''''..;%&!                                                                                                        `
                                                                                                              .%#%:|&!                  `%#|`..'''`''`.:|$!                                                                                                        `
                                                                                                             `|$|. .!|:                :%@&:.`'`. .`'`.:|&|.                                                                                                       `
                                                                                                            `|@|`...:$$'              :$#&!``':.  .`'`.;%&|.                                                                                                       `
                                                                                                           '$@$'.''``|#&;.           |##%'.':'.   .`'`.:%&%.                                                                                                       `
                                                                                                           !@%:``'''`:$#@;.         ;@#%'``'`     .`'`.'|@$.                                                                                                       `
                                                                                                          `%$:.`'''''``!&#&'   .;%&##|```''.      .`'`.'!@&'                                                                                                       `
                                                                                                          '%%'.`'''''```:$#&|%$@#@$!'..`''`       .''```;&&'                                                                                                       `
                                                                                                          :%|'.`''''''```:|@#&$;'``.``'''`        .`'```:&&:                                                                                                       `
                                                                                                         :$&|'.`'''''''''``....`''''''''`         .`''`.`%$;                                                                                                       `
                                                                                                      .'|@#%:``'''``''''''''`''''``''''`.         .`''`.`!$|`                                                                                                      `
                                                                                                     ;&##$!'``''''''''''''''''''`''''''`.         .`''`.`;$$:.                                                                                                     `
                                                                                                 '!&#@|'...'''''''''''`''''''''''''''''''::`      .`'''```!$|`                                                                                                     `
                                                                                               :&##@%'..`''''''''''''''''''''''''''''''''''''.    .''''```;&&:                                                                                                     `
                                                                                           '|@#@|'. .':'''''''''''''''''''''''''''''''``'''''''...`'''''`.'$#|`                                                                                                    `
                                                                                       .';|@#&|' .`'''``'`''''''''''''''''''''''''''''''''```'''''''''''`.`%@$;                                                                                                    `
                                                                                .`':!%$&&&$;'`.`'''''```''`..``'''''''''''''''''''''''''''''''`''''''''''``!&#$`                                                                                                   `
                                                                 ...`':;!||%$&@###&%!'.   .`:'''''``.;%$$$&&%!'``''''''''''''''''''''''''''''''''''`''''''`'!&#|.                                                                                                  `
                                                             '|$&&&@@#@@&&&&$%|;'`...```'''''''''``'!%!'`:%###@!```'''''''''''''''''''''''''''''''''''''''``'|@&|`                                                                                                 `
                                                            `|#######$'...    .``'''''''''''''''`';$&: `!$#####&!'``'''''''''''''''''''''''''''''''''''''''`.:%##%:                                                                                                `
                                                            '%###@@&|`                    .``'''`'!$&: '$@#####|'```''''''''''''''''''''''''''''''''''''''''`.`%##@%'                                                                                              `
                                                            '%#%'``.                          .`'``:||;;|@##&%;`.`'''''''''''''''''''''''''''''''''''''''''''`.;$###$`                                                                                             `
                                                            `|@!                                .':'``'''`....`''''''`.``''''''''''''''''''''''''''''''''``''''`.;$##&!.                                                                                           `
                                                            .!@|.                                 `':'``````''''''`.      .`'''''''''''''''''''''''''''''''''''``.:%@#@!`                                                                                          `
                                                             ;@%`                                   `':'''''`''''.          .''''''''''''''''''''''''''''''''''''`.`;@##|`                                                                                         `
                                                             :&&;.                                    `''::'''''`             .''''''''''''''''''''''''''''''''''''`.:$@@%'                                                                                        `
                                                             `|&&;                                       ..``'''.              .`'''''''''''''''''''''''''''''''''''`.'%@@|.                                                                                       `
                                                              `$#&;.                                                             .`''''''''''''''''''''''''''''''''''..:%@$`                                                                                       `
                                                               :@##$;.                                                             .`''''''''''''''''''''''''''''''`````;&@:                                                                                       `
                                                               `%####|`                                                               ..``'''''''''''''''''''''''''''``.:$&;.           `'::''``.                                                                  `
                                                                 `%@####@%!`                                                              .`'''''''''''''''''''''''''``.'|&%'        :%&&$$$$$&&@&&&%!:`             .``.                                          `
                                                                  .!&######@&$$%||!!!!;:'.                                                .`''''''''''''''''''''''''''`.'|&$;.     .:%$%|!!|||||||%%$&&$%!;:''::;;!!|$$!'                                          `
                                                                    `|####&;':;!|||||!:`.                                                .``'''''''''''''''''''''''''``.'!&$;.    :|$||||||||||||||||||%%%%%%%$$%%%|%$%'                                           `
                                                                      `!&##@%;`                                                     .`''''''''''''''''''''''''''''''''`.`!$&!. .;$&$%|%%%%||||||||||||||||!!!!!!!!!%$!'                                            `
                                                                        .:!&##@%:`.                                                .''''''''''''''''''''''''''''''''''`.`;$&|'`|@@@&&&&@&&&$$%%||||||||||||!!!!!!|$$|`                                             `
                                                                           `:|&##@$%!'                                            .`'''''''''''''''''''''''''''''''```'':|$@&$%$$$&@@@&&@@@@@@&$%||||||!!!!!!!!%$&$!'                                              `
                                                                               '%@####@%;.                                       .`''''''''''''''''''''''''''''````';%&@@@@@@&$|!|%&&$%|||%$&@@@&$%|||||||||%$&&%;.                                                `
                                                                                 `:|&####$;.                                     `'''''''''''''''''''''''''''```:;|$&@&$%%%&@@&%||%&&$%|||||%%$@@@&$%%$$&&@@@@$;.                                                  `
                      .:!|!'                                                         `!%&@@%;::;:'.                             .''''''''''''''''''''''''```':!$#@&$%|!||||$&@&%||%&&%||!||||||%$$&@@@&%|;:`                                                       `
                    ;$@##@@##|.                                                       '|&$$$%%%$$%%|!;:''`.                     `'''`''''''''''''''''`'''':!%$&&$%|||||||||$&@@%||%&&%||||||||||||%$&&%:.                                                          `
                  '$#@|.   .|#&:                                                       :%&$|!!!!|||%%%$$%%%|!!;'`.             .'''''''''''''''''````':!|%$&&$%%|||||||||||%&@&%||%$$%||||||||||||||%&&%:.                                                         `
                 '$#%`      .|#&'         .....                                         .|@$|!!!!!!!!!!!!|||%%$$$$$$%!:'`.      `'``````````````'':!|$&&&$%||||||||||||||||%$@#@@@@#@$%%|||||||||||||||$&&|`                                                       `
                .|@@!        :&@;                                                      `!%$&&$%|!!!!!!!!!!!!!!!||||%%$$$$%|!:''::::::::::;;;!!!!|%%$$$%|||||||||||||||||%%%$&&$%%%%%&@@$$%||||||||||||||%%$%!'.                                                    `
                `%#&:        .|##$;`.      ...                   :!%%%%%$$$%%%%$&@@@@@####@&@&$%!!!!!!!!!!!!!!!!!!!!!||%%%%$$$$$$$$$$$$$$$$$$$%%%%|||||||||||||||||%%%$$&&&|;;;;::::;%@@@&$%|||||||||||||||%%%|!;:':;;'.                                           `
                '$#|.          :$@###########################@@&@@@@@&&$$$$$$$$$%%$$&$$$&&%|%&&$||!!!!!!!!!!!!!!!!!!!!!!!!||||||||||%%%||||||||||||||||||||||||%$$&&&&&&$!:'''':!;;::';%@@&%|||||||||||||||!!||%$&@@%'                                             `
                '&#!.                   ...``````'''''''`'''';!%&&|''''''''''''''''''''''::::%&$%%|!!!!!!!!!!!!!!!!!!!!!!!!||||||||||||||||||||||||||||||||%%$&&&$$&&&%!:'``''':::::'``;%&@$%||||||||!||||||%$&@&%!'.                                              `
                .%#$'                                          ;&@|:''''''''''''''''''''''''';%$$%%|!!!!!!!!|||!!!!!!!!!!||||||||||||||||||||||||||||||%$$&@@@&$$&@&|:';!;:''''::''''''';$&&&&$$%%%%%%%$$$&@@&%'                                                   `
                '$#@|`                                         ;&@|:''''''''''''''''''''''''':!$&$%%|!!!!!!!!!!!!!!!!!!!!!!||||||||||||||||||||||||%%$$&&&&&&&&&&$|;::::::::'''''''''''`';%&@&&&&&&&$&&@&&&@#&:                                                    `
             `';;%@#@%`                                        ;&@|:'''''''''''''''''''''''''':%@&$$%|!!!!!!!!!!!!!!!!|||||||||||||||||||||||||%%$$&&&&$$$$&&$$|;:'``''''''::''''''''''``.';!!!!!!;;:;:'`.`!&@|`                                                   `
            `:;;;:;!$@#@%;`                                    :&@|:'''''''''''''''''''''''''``;%&&$$%|!!!!!!!!!!!!!!!|||%%%$$$$$$$$%%%%%%$$$&&&&&&$$%%$&@&%%@#$!!;:`'''''''''''''''''''''`''''''''''''':!$@##&`                                                   `
            .':;;;;:::!%&###@&|'                              .!@@|:''''''''''''''''''''''''''`';%&&&$%||!!!!!!!!!!!!!!!!||||%%%$$$$$$$$$$&&$$$$%%||%&&&$%%%$@#@$%&&@#$!:''''''''''''::::'::::::::::!$@#####@;                                                     `
            .`::;;;;;:::::;!%$@##@@%;.                         ;@@|'''''''''''''''''''''''''''''';%&@&$$%||!!!!!!!!!!!!!!!!!!||||||||%%%%%%%%||||%$&&$$$$$$$$@#@%!;%@@@#@&|:'''''::::'''':::::;!%&@###&|;|&#$`                                                     `
            .'::;;;;;;;:::::::::;|&@###@$|:`                   :&@|:''''''''''''''''''''''''::':;%@@@@@&$$%|!!!!|!!!!!!||!!||||||||||||||||||||%&&&&$$$$$$$%$@#@%!;!$#####@#&!''::''''':;|&@######&;.     !#$`                                                     `
            .'::;;;;;;::::::::::`     '!$@####@&%;`.           ;&@%:''''''''''''''''''''''''''':!%$$$&&&&&$%%||!!!!!!!!|||||||!|||||||||||||||%&@&$&&$$$$$$%$@#@|;;|$@@; `|&##@&|::;|$&@####$;`          `|@%`                                                     `
            .'::;;;;;;:::::::::::'`.         `'!$@###@@&%;`   .!@#$;'''''''''''''''''''''''':'';%&@&$$$&&&&&$$%%|!!!!||!!!|||||||||||||||||||$@&&&$$$$$$$$$%$@#&!::!%@@|.     :&#######@%;.              `$@!                                                      `
            .':::;;;:;;;:::::::::'``...             `!$@@####@@@@#$:'''''''''''''''''''''''':'';%@@$$$$$$$$&&&&$%%|||!!||||||||||||||||||||$&&&&#@$$$$$$$$%$&##$!:';%&#%.       '$@@|.  .                |#$`                                                      `
            `':::;;;;;::::::::::''```..  ...              .'!|&###%:'''''''''''''''''''''''''''!&#@$$$$$$%%&@@&@@&$%%|||!|||||||||||||||||%&&%!$#@$$$$$$$$%$&##$!:';%@@;         !@&:                   ;@&;                                                       `
            `':::;::;;;:::::::''''``....                       !@#%:'''''''''''''''''''''''''''!&@&$$$$$$$$@&|:!%&&@&$%%|||||||||||||||||$&$!``%#&$$$$$$$$$$&##$!:':|@@;        ;@@;                   !@&;                                                        `
            `':::;;;;;::::::::'''```.. .                       `$#&;'''''''''''''''''''''''''''|@#&$$$$$$$&@$;''':;|$&&$$%%||||||||||||%&@$;'';$#&$$$$$$$$$$&##$;'':|@@;       !@@;                  '&#&:                                                         `
            `''::::::::::::::''''``...                          `$#@&%|;::::::::::::::::'''''':%##&$$$$$$$$@&;'':''':!$&&&$$%||||||%%$&&%;'''';&#&$$$$%$$$$$@##$;:':|@&:      !@@;                  ;@#$'                                                          `
            `''::::::::::::::''''``...                            :$##@@@@@@@@&&&&&&&@&&$%|!;:;$##&$$$$$$$$&|'':::''''';%&&$$$$%%%$&&%!:''''''!&#&$$$$$$$$$$&##%;:':|@&'    `$#$`                 .|#@!                                                            `
            .`''::::::::::::''''````..                               .....';|%$&@@@@@@@####@$|!&#@$$$$$$$$$&!''::''''''':|$&&$$&@&$!;:'''''''';$#&$$$$$$$$$%&##%:'':%@@:   :&@;                  ;&#$`                                                             `
            .``'':::::::::''''''```...                                                 '%###$|!&#@$$$$$$$$$$!''::''''''''`'|&@#$;'`''''''''''';$#@$%%$$$$$$$&#@|:'':%@&'  !@&:                `|@#|.                                                               `
            .``'''::::::::'''''```...                                                   :&#@$||@#&$$$$$$$$&&!''''''''''''''':;;''''''''''''''';$#@$$%$$$$$$$&#@|:':;%@#$%&#%.               `%@#$'                                                                 `
            ..``'''''::::''''''``....                                                   '$#@%%%@#&$$$$$$$$&&|'':'''''''''''''''''''''''''''''':%#@$$%%$$$$$$&#@|;!|$@####&:               `%@#&:                                                                   `
            ..```''''''''''''````...                                                    `%#&%%$@#&$$$$$$$$&@|'':'''''''''''''''''''''''''''''':%#@$$%%$$$$$$@#@$$&##@!.                 .|@#@!                                                                     `
             ..```''''''''``````...                                                     `%#&||$##&$$$$$$$$&&|:'''''''''''''''''''''''''''''''':$#@$$%%$$$$%$@##@##&:                  .|@#@;                                                                       `
             ...``````````````...                                                       `$@&|%&##&$$$$$$$$&@|''''''''''''''''''''''''''''''''';$#@$$$$%$$%%&####&;                  .|@#$'                                                                         `
              ....`````````.....                                                        '$@$|$&##&$$$$$$$$&@|''''''''''''''''''''''''''''''''';&#@$$$$%$$$$$&##@!                 .|@#$`                                                                           `
                ...............                                                         ;&@$|$@#@$$$$$$$$$&@%:'''''''''''''''''''''''''''''''';&#@$$$$$$$$$$&##&:        `      .|@#|.                                                                             `
                   .......                                                              !@@%|&##@$$$$$$$$$@@|''''''''''''''''''''''''''''''''';&#&$%%$$$$$$$&##@!.        .```.`:$@#%.                                                                             `
                                                                                       .|#&%|&##&$$$$$$$$$@&;''''''''''''''''''''''''''''''''':$#&$%%$$$$$$%&##@;               `|@@|                                                                              `
                                                                                       '$#&||&##&$$$$$$$$&@$:''''''''''''''''''''''''''''''''':%@&$%%$$$$$$%&###&;           ...`!@#%.                                                                             `
                                                                                       ;&@$!|&##&$$$$$$$$&#$:''''''''''''''''''''''''''''''''':$#&$$$$$$$$%$&#####$:             ;@#&`                                                                             `
                                                                                       !@@%;|&#@$$$$$$$$$&#$:''''''''''''''''''''''''''''''''':$#&$$$$$$$$$$@##&%%@######@$||$&@##$'                                                                               `
                                                                                       !@@|;|&#&$$$$$$$$$&#$:''''''''''''''''''''''''''''''''':%#@$$$$%$$$$$@#@|:'':;%@&&&@@@&%!'                                                                                  `
                                                                                       !@@!:|@#&$$$$$$$$$@#%:''''''''''''''''''''''''''''''''''|@@$$$$$$$$$$@#@!:''``|@;                                                                                           `
                                                                                       !&%::|&#@$$$$%$$$$@#|'''''''''''''''''''''''''''''''''':%##$$$$$$$$$&@#@|:::``%@!                                                                                           `
                                                                                       `''``:!|%$%|!!!!!||%|;''`..````.````````````````''''''';|%%||!!;;!!|%%%|:`   .%@!                                                                                           `
                                                                                                                                                                                                                                                                   `
                                                                                                                                                                                                                                                                   `
                                                                                                                                                                                                                                                                   `
                                                                                                                                                                                                                                                                   `
                                                                                                                                                                                                                                                                   `
                                                                                    .                                                                             .`.                                                                                              `
                                                         ......:!:. ...           `|$'         `%|'!%!;|%$$$$$$:    `|$%%$$$$$$%$$%'   :$%%%%%%$$$%%$$%$|.        :$$;;;;;'                         .!%`   .........                                               `
                                                        ':;!!!|$%!!!!!;'.  `|%|!!!|%$%!!!!|!.  .:$@$;.    '%$;`     '%|'`.`%@;.`'%&:    .     `|%:     ..    ````';&$:':::'`.                        ;|`  :%$&&$%%&@!                                              `
                                                          .;!::;;:':|!.    '&$;::'. `;!'..;|`  ;%%%!;;|%%%$&%%%|;`  '%%;:':$@!':;$&:         `||'.          '$$;:;|$%;;!!:!%;.                      .;|'``. .!!.  :&!                                              `
                                                          `%@&&&&&&&@$`      ..'!%%:`;;`       ..`;$%`  .`!!`       `|!'``'%&;``'%&:       `;|&%`:%%;.      `$&|%$&@&|;;:`'`                      `%&&$!;;` .%%. `;%:                                              `
                                                          `||::''':;%%`      :%!'`` `!:         .;&#$` `:$&;...      ':'::|%!:::::'.     .:%|':|`  :|!'     '%%. .'!|:'';|!.  .!$%%%%%%%%%%%%%%!`    ;!.    `$%. `;!'                                              `
                                                          `%@&&&&&&&@%`    `;;;!!;'`:%!'`'':'  ;&%;$$;%|!$&%%!!!.  .:%$%%$&@&$%%%$%!'  :%$%'  '!'   .|&|'  .:|!.  `||:!!'.                           :!..;:`;$!  `;!'                                              `
                                                       `::;|!;:::::;|%;:;` .`''''':%&|;:'''``  ''.`%$!$!`$%!&;'!`    .. `;%!. ...;%!'  `'.    '!'     .    '!!;!|''$|'!;':!;.                       .|&$|!:|$;.  `;!'                                              `
                                                        .``;%%!'.'!%$!:`.      `:!||:'!%%!:.     '|%|!&|:$$|&!;|:     .'!$%`     !&;.         '!'          !&:.:|:'&|'!;:;;`                        .;;``:||:  .:%$;`                                              `
                                                       .:$&!'.     .`!@%'  '$@@$!'.     `!$%'  '$#&;'|%%|%%%%||%|' ;$&&%!`    .%@&%'          :|'         '%%`'&#@@##@@@@@@#$`                         `|@%.  .%@$;.                                               `
                                                                                                                                                                                                                                                                   `
                                                                                                                                                                                                                                                                   `
                                                                                                                                                                                                                                                                   `
                                                                                                                                                                                                                                                                   `
                                                                                                                                                                                                                                                                   `
********************************************************************************************************************************************************************************************************************************************************************
*******************************************************************************************************************************************************************************************************************************************************************/
/*********************
*@Author:   CKang    *
*@Language: C++11    *
*********************/
#include<bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
//#define DEBUG
#define RI register int
#define endl "\n"

using namespace std;

typedef long long ll;
//typedef __int128 lll;
//const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-9;
const int INF = 0x3fffffff;
const ll LINF = 0x3fffffffffffffff;

inline ll read(){
    long long x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}

void solve(){
    ll a=read(),b=read(),c=read(),k=read(),sum=0;
    if(k>a){
        sum+=a;
        k-=a;
        if(k>b){
            k-=b;
            if(k>c){
                return ;
            }
            else {
                printf("%d\n",sum-k);
            }
        }
        else {
            printf("%d\n",sum);
        }
    }
    else {
        printf("%d\n",k);
    }
    return ;
}

int main()
{
#ifdef DEBUG
    freopen("input.in", "r", stdin);
    //freopen("output.out", "w", stdout)
#endif
    //cout.tie(0);
    //for(ll t=read();t;t--)
        solve();
#ifdef DEBUG
    printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Fuck You !" << endl;
    return 0;
}


Measured memory_bytes: 3760