import java.util.Scanner;

public class Main {

    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int A = Integer.parseInt(sc.next());
        int B = Integer.parseInt(sc.next());
        int C = Integer.parseInt(sc.next());
        int K = Integer.parseInt(sc.next());
        int max=0;


        if( K < A){
            max = K;
            K=0;
        }else{
            for(int i=0; i<A; i++){
                max++;
                K--;
            }
        }
        if(K > 0){
            K-=B;
        }
        if(K > 0){
            while(K > 0){
                K--;
                max--;
            }
        }
        A=C-1;

        System.out.println(max);

        sc.close();
    }
    
} import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int A = Integer.parseInt(sc.next());
        int B = Integer.parseInt(sc.next());
        int C = Integer.parseInt(sc.next());
        int K = Integer.parseInt(sc.next());
        int max=0;


        if( K < A){
            max = K;
            K=0;
        }else{
            for(int i=0; i<A; i++){
                max++;
                K--;
            }
        }
        if(K > 0){
            K-=B;
        }
        if(K > 0){
            while(K > 0){
                K--;
                max--;
            }
        }
        A=C-1;

        System.out.println(max);

        sc.close();
    }
    
} Main public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int A = Integer.parseInt(sc.next());
        int B = Integer.parseInt(sc.next());
        int C = Integer.parseInt(sc.next());
        int K = Integer.parseInt(sc.next());
        int max=0;


        if( K < A){
            max = K;
            K=0;
        }else{
            for(int i=0; i<A; i++){
                max++;
                K--;
            }
        }
        if(K > 0){
            K-=B;
        }
        if(K > 0){
            while(K > 0){
                K--;
                max--;
            }
        }
        A=C-1;

        System.out.println(max);

        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);
        int A = Integer.parseInt(sc.next());
        int B = Integer.parseInt(sc.next());
        int C = Integer.parseInt(sc.next());
        int K = Integer.parseInt(sc.next());
        int max=0;


        if( K < A){
            max = K;
            K=0;
        }else{
            for(int i=0; i<A; i++){
                max++;
                K--;
            }
        }
        if(K > 0){
            K-=B;
        }
        if(K > 0){
            while(K > 0){
                K--;
                max--;
            }
        }
        A=C-1;

        System.out.println(max);

        sc.close();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int A = Integer.parseInt(sc.next()); A Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int B = Integer.parseInt(sc.next()); B Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int C = Integer.parseInt(sc.next()); C Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int K = Integer.parseInt(sc.next()); K Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int max=0; max 0 if( K < A){
            max = K;
            K=0;
        }else{
            for(int i=0; i<A; i++){
                max++;
                K--;
            }
        } K < A K A {
            max = K;
            K=0;
        } max = K max K K=0 K 0 {
            for(int i=0; i<A; i++){
                max++;
                K--;
            }
        } for(int i=0; i<A; i++){
                max++;
                K--;
            } int i=0; int i=0; i 0 i<A i A i++ i++ i {
                max++;
                K--;
            } {
                max++;
                K--;
            } max++ max K-- K if(K > 0){
            K-=B;
        } K > 0 K 0 {
            K-=B;
        } K-=B K B if(K > 0){
            while(K > 0){
                K--;
                max--;
            }
        } K > 0 K 0 {
            while(K > 0){
                K--;
                max--;
            }
        } while(K > 0){
                K--;
                max--;
            } K > 0 K 0 {
                K--;
                max--;
            } K-- K max-- max A=C-1 A C-1 C 1 System.out.println(max) System.out.println System.out System System.out max sc.close() sc.close sc String args[] args public class Main {

    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int A = Integer.parseInt(sc.next());
        int B = Integer.parseInt(sc.next());
        int C = Integer.parseInt(sc.next());
        int K = Integer.parseInt(sc.next());
        int max=0;


        if( K < A){
            max = K;
            K=0;
        }else{
            for(int i=0; i<A; i++){
                max++;
                K--;
            }
        }
        if(K > 0){
            K-=B;
        }
        if(K > 0){
            while(K > 0){
                K--;
                max--;
            }
        }
        A=C-1;

        System.out.println(max);

        sc.close();
    }
    
} public class Main {

    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int A = Integer.parseInt(sc.next());
        int B = Integer.parseInt(sc.next());
        int C = Integer.parseInt(sc.next());
        int K = Integer.parseInt(sc.next());
        int max=0;


        if( K < A){
            max = K;
            K=0;
        }else{
            for(int i=0; i<A; i++){
                max++;
                K--;
            }
        }
        if(K > 0){
            K-=B;
        }
        if(K > 0){
            while(K > 0){
                K--;
                max--;
            }
        }
        A=C-1;

        System.out.println(max);

        sc.close();
    }
    
} Main
import java.util.Scanner;

public class Main {

    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int A = Integer.parseInt(sc.next());
        int B = Integer.parseInt(sc.next());
        int C = Integer.parseInt(sc.next());
        int K = Integer.parseInt(sc.next());
        int max=0;


        if( K < A){
            max = K;
            K=0;
        }else{
            for(int i=0; i<A; i++){
                max++;
                K--;
            }
        }
        if(K > 0){
            K-=B;
        }
        if(K > 0){
            while(K > 0){
                K--;
                max--;
            }
        }
        A=C-1;

        System.out.println(max);

        sc.close();
    }
    
}