import java.util.Scanner;

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();
        
        if (A >= K) {
            System.out.println(K);
        } else if (A + B >= K) {
            System.out.println(A);
        } else if (A + B + C >= K) {
            System.out.println(A - (C - ((A + B + C) - K)));
        }
    }
}