import java.util.Scanner;

public class Main {
    public static void main(final String args[]) {
        Scanner scan = new Scanner(System.in);
        int A = scan.nextInt();
        int B = scan.nextInt();
        int C = scan.nextInt();
        int K = scan.nextInt();
        int sum = 0;
        if(A >= K){
            sum = K;
        }else if(A < K){
            sum = A;
            K = K - A;
            if(B >= K){
                
            }else if(B < K){
                //sum = sum + B;
                K = K - B;
                if(C >= K){
                    sum = sum - K;
                }else{
                    sum = sum - C;
                }
            }
        }
        System.out.println(sum);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(final String args[]) {
        Scanner scan = new Scanner(System.in);
        int A = scan.nextInt();
        int B = scan.nextInt();
        int C = scan.nextInt();
        int K = scan.nextInt();
        int sum = 0;
        if(A >= K){
            sum = K;
        }else if(A < K){
            sum = A;
            K = K - A;
            if(B >= K){
                
            }else if(B < K){
                //sum = sum + B;
                K = K - B;
                if(C >= K){
                    sum = sum - K;
                }else{
                    sum = sum - C;
                }
            }
        }
        System.out.println(sum);
    }
} Main public static void main(final String args[]) {
        Scanner scan = new Scanner(System.in);
        int A = scan.nextInt();
        int B = scan.nextInt();
        int C = scan.nextInt();
        int K = scan.nextInt();
        int sum = 0;
        if(A >= K){
            sum = K;
        }else if(A < K){
            sum = A;
            K = K - A;
            if(B >= K){
                
            }else if(B < K){
                //sum = sum + B;
                K = K - B;
                if(C >= K){
                    sum = sum - K;
                }else{
                    sum = sum - C;
                }
            }
        }
        System.out.println(sum);
    } main {
        Scanner scan = new Scanner(System.in);
        int A = scan.nextInt();
        int B = scan.nextInt();
        int C = scan.nextInt();
        int K = scan.nextInt();
        int sum = 0;
        if(A >= K){
            sum = K;
        }else if(A < K){
            sum = A;
            K = K - A;
            if(B >= K){
                
            }else if(B < K){
                //sum = sum + B;
                K = K - B;
                if(C >= K){
                    sum = sum - K;
                }else{
                    sum = sum - C;
                }
            }
        }
        System.out.println(sum);
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int A = scan.nextInt(); A scan.nextInt() scan.nextInt scan int B = scan.nextInt(); B scan.nextInt() scan.nextInt scan int C = scan.nextInt(); C scan.nextInt() scan.nextInt scan int K = scan.nextInt(); K scan.nextInt() scan.nextInt scan int sum = 0; sum 0 if(A >= K){
            sum = K;
        }else if(A < K){
            sum = A;
            K = K - A;
            if(B >= K){
                
            }else if(B < K){
                //sum = sum + B;
                K = K - B;
                if(C >= K){
                    sum = sum - K;
                }else{
                    sum = sum - C;
                }
            }
        } A >= K A K {
            sum = K;
        } sum = K sum K if(A < K){
            sum = A;
            K = K - A;
            if(B >= K){
                
            }else if(B < K){
                //sum = sum + B;
                K = K - B;
                if(C >= K){
                    sum = sum - K;
                }else{
                    sum = sum - C;
                }
            }
        } A < K A K {
            sum = A;
            K = K - A;
            if(B >= K){
                
            }else if(B < K){
                //sum = sum + B;
                K = K - B;
                if(C >= K){
                    sum = sum - K;
                }else{
                    sum = sum - C;
                }
            }
        } sum = A sum A K = K - A K K - A K A if(B >= K){
                
            }else if(B < K){
                //sum = sum + B;
                K = K - B;
                if(C >= K){
                    sum = sum - K;
                }else{
                    sum = sum - C;
                }
            } B >= K B K {
                
            } if(B < K){
                //sum = sum + B;
                K = K - B;
                if(C >= K){
                    sum = sum - K;
                }else{
                    sum = sum - C;
                }
            } B < K B K {
                //sum = sum + B;
                K = K - B;
                if(C >= K){
                    sum = sum - K;
                }else{
                    sum = sum - C;
                }
            } K = K - B K K - B K B if(C >= K){
                    sum = sum - K;
                }else{
                    sum = sum - C;
                } C >= K C K {
                    sum = sum - K;
                } sum = sum - K sum sum - K sum K {
                    sum = sum - C;
                } sum = sum - C sum sum - C sum C System.out.println(sum) System.out.println System.out System System.out sum final String args[] args public class Main {
    public static void main(final String args[]) {
        Scanner scan = new Scanner(System.in);
        int A = scan.nextInt();
        int B = scan.nextInt();
        int C = scan.nextInt();
        int K = scan.nextInt();
        int sum = 0;
        if(A >= K){
            sum = K;
        }else if(A < K){
            sum = A;
            K = K - A;
            if(B >= K){
                
            }else if(B < K){
                //sum = sum + B;
                K = K - B;
                if(C >= K){
                    sum = sum - K;
                }else{
                    sum = sum - C;
                }
            }
        }
        System.out.println(sum);
    }
} public class Main {
    public static void main(final String args[]) {
        Scanner scan = new Scanner(System.in);
        int A = scan.nextInt();
        int B = scan.nextInt();
        int C = scan.nextInt();
        int K = scan.nextInt();
        int sum = 0;
        if(A >= K){
            sum = K;
        }else if(A < K){
            sum = A;
            K = K - A;
            if(B >= K){
                
            }else if(B < K){
                //sum = sum + B;
                K = K - B;
                if(C >= K){
                    sum = sum - K;
                }else{
                    sum = sum - C;
                }
            }
        }
        System.out.println(sum);
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(final String args[]) {
        Scanner scan = new Scanner(System.in);
        int A = scan.nextInt();
        int B = scan.nextInt();
        int C = scan.nextInt();
        int K = scan.nextInt();
        int sum = 0;
        if(A >= K){
            sum = K;
        }else if(A < K){
            sum = A;
            K = K - A;
            if(B >= K){
                
            }else if(B < K){
                //sum = sum + B;
                K = K - B;
                if(C >= K){
                    sum = sum - K;
                }else{
                    sum = sum - C;
                }
            }
        }
        System.out.println(sum);
    }
}