import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        String[] in = str.split(" ",0);
        int a = Integer.valueOf(in[0]);
        int b = Integer.valueOf(in[1]);
        int c = Integer.valueOf(in[2]);
        int k = Integer.valueOf(in[3]);
        int out;
        if(k <= (a + b + c) && (a + b + c) <= 2*Math.pow(10 , 9)){
            if(k - a >= 0){
                out = a;
                k = k - a;
                if(k - b >= 0){
                    out = out - (k-b);
                }
            }else {
                out = k;
            }
            System.out.println(out);
        }
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        String[] in = str.split(" ",0);
        int a = Integer.valueOf(in[0]);
        int b = Integer.valueOf(in[1]);
        int c = Integer.valueOf(in[2]);
        int k = Integer.valueOf(in[3]);
        int out;
        if(k <= (a + b + c) && (a + b + c) <= 2*Math.pow(10 , 9)){
            if(k - a >= 0){
                out = a;
                k = k - a;
                if(k - b >= 0){
                    out = out - (k-b);
                }
            }else {
                out = k;
            }
            System.out.println(out);
        }
    }
} Main public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        String[] in = str.split(" ",0);
        int a = Integer.valueOf(in[0]);
        int b = Integer.valueOf(in[1]);
        int c = Integer.valueOf(in[2]);
        int k = Integer.valueOf(in[3]);
        int out;
        if(k <= (a + b + c) && (a + b + c) <= 2*Math.pow(10 , 9)){
            if(k - a >= 0){
                out = a;
                k = k - a;
                if(k - b >= 0){
                    out = out - (k-b);
                }
            }else {
                out = k;
            }
            System.out.println(out);
        }
    } main {
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        String[] in = str.split(" ",0);
        int a = Integer.valueOf(in[0]);
        int b = Integer.valueOf(in[1]);
        int c = Integer.valueOf(in[2]);
        int k = Integer.valueOf(in[3]);
        int out;
        if(k <= (a + b + c) && (a + b + c) <= 2*Math.pow(10 , 9)){
            if(k - a >= 0){
                out = a;
                k = k - a;
                if(k - b >= 0){
                    out = out - (k-b);
                }
            }else {
                out = k;
            }
            System.out.println(out);
        }
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) String str = scan.nextLine(); str scan.nextLine() scan.nextLine scan String[] in = str.split(" ",0); in str.split(" ",0) str.split str " " 0 int a = Integer.valueOf(in[0]); a Integer.valueOf(in[0]) Integer.valueOf Integer in[0] in 0 int b = Integer.valueOf(in[1]); b Integer.valueOf(in[1]) Integer.valueOf Integer in[1] in 1 int c = Integer.valueOf(in[2]); c Integer.valueOf(in[2]) Integer.valueOf Integer in[2] in 2 int k = Integer.valueOf(in[3]); k Integer.valueOf(in[3]) Integer.valueOf Integer in[3] in 3 int out; out if(k <= (a + b + c) && (a + b + c) <= 2*Math.pow(10 , 9)){
            if(k - a >= 0){
                out = a;
                k = k - a;
                if(k - b >= 0){
                    out = out - (k-b);
                }
            }else {
                out = k;
            }
            System.out.println(out);
        } k <= (a + b + c) && (a + b + c) <= 2*Math.pow(10 , 9) k <= (a + b + c) k (a + b + c) a + b + c a b c (a + b + c) <= 2*Math.pow(10 , 9) (a + b + c) a + b + c a b c 2*Math.pow(10 , 9) 2 Math.pow(10 , 9) Math.pow Math 10 9 {
            if(k - a >= 0){
                out = a;
                k = k - a;
                if(k - b >= 0){
                    out = out - (k-b);
                }
            }else {
                out = k;
            }
            System.out.println(out);
        } if(k - a >= 0){
                out = a;
                k = k - a;
                if(k - b >= 0){
                    out = out - (k-b);
                }
            }else {
                out = k;
            } k - a >= 0 k - a k a 0 {
                out = a;
                k = k - a;
                if(k - b >= 0){
                    out = out - (k-b);
                }
            } out = a out a k = k - a k k - a k a if(k - b >= 0){
                    out = out - (k-b);
                } k - b >= 0 k - b k b 0 {
                    out = out - (k-b);
                } out = out - (k-b) out out - (k-b) out (k-b) k b {
                out = k;
            } out = k out k System.out.println(out) System.out.println System.out System System.out out String[] args args public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        String[] in = str.split(" ",0);
        int a = Integer.valueOf(in[0]);
        int b = Integer.valueOf(in[1]);
        int c = Integer.valueOf(in[2]);
        int k = Integer.valueOf(in[3]);
        int out;
        if(k <= (a + b + c) && (a + b + c) <= 2*Math.pow(10 , 9)){
            if(k - a >= 0){
                out = a;
                k = k - a;
                if(k - b >= 0){
                    out = out - (k-b);
                }
            }else {
                out = k;
            }
            System.out.println(out);
        }
    }
} public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        String[] in = str.split(" ",0);
        int a = Integer.valueOf(in[0]);
        int b = Integer.valueOf(in[1]);
        int c = Integer.valueOf(in[2]);
        int k = Integer.valueOf(in[3]);
        int out;
        if(k <= (a + b + c) && (a + b + c) <= 2*Math.pow(10 , 9)){
            if(k - a >= 0){
                out = a;
                k = k - a;
                if(k - b >= 0){
                    out = out - (k-b);
                }
            }else {
                out = k;
            }
            System.out.println(out);
        }
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        String[] in = str.split(" ",0);
        int a = Integer.valueOf(in[0]);
        int b = Integer.valueOf(in[1]);
        int c = Integer.valueOf(in[2]);
        int k = Integer.valueOf(in[3]);
        int out;
        if(k <= (a + b + c) && (a + b + c) <= 2*Math.pow(10 , 9)){
            if(k - a >= 0){
                out = a;
                k = k - a;
                if(k - b >= 0){
                    out = out - (k-b);
                }
            }else {
                out = k;
            }
            System.out.println(out);
        }
    }
}
