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()); // +1
        int b = Integer.parseInt(sc.next()); // 0
        int c = Integer.parseInt(sc.next()); // -1
        int k = Integer.parseInt(sc.next());

        // 出力
        int num = 0;
        int count = 0;
        outside: {
            for (int i = 0; i < a; i++) {
                if (count == k) {
                    break outside;
                }
                num++;
                count++;
            }
            for (int i = 0; i < b; i++) {
                if (count == k) {
                    break outside;
                }
                count++;
            }
            for (int i = 0; i < c; i++) {
                if (count == k) {
                    break outside;
                }
                num--;
                count++;
            }
        }
        System.out.println(num);
    }
} 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()); // +1
        int b = Integer.parseInt(sc.next()); // 0
        int c = Integer.parseInt(sc.next()); // -1
        int k = Integer.parseInt(sc.next());

        // 出力
        int num = 0;
        int count = 0;
        outside: {
            for (int i = 0; i < a; i++) {
                if (count == k) {
                    break outside;
                }
                num++;
                count++;
            }
            for (int i = 0; i < b; i++) {
                if (count == k) {
                    break outside;
                }
                count++;
            }
            for (int i = 0; i < c; i++) {
                if (count == k) {
                    break outside;
                }
                num--;
                count++;
            }
        }
        System.out.println(num);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 入力
        int a = Integer.parseInt(sc.next()); // +1
        int b = Integer.parseInt(sc.next()); // 0
        int c = Integer.parseInt(sc.next()); // -1
        int k = Integer.parseInt(sc.next());

        // 出力
        int num = 0;
        int count = 0;
        outside: {
            for (int i = 0; i < a; i++) {
                if (count == k) {
                    break outside;
                }
                num++;
                count++;
            }
            for (int i = 0; i < b; i++) {
                if (count == k) {
                    break outside;
                }
                count++;
            }
            for (int i = 0; i < c; i++) {
                if (count == k) {
                    break outside;
                }
                num--;
                count++;
            }
        }
        System.out.println(num);
    } main {
        Scanner sc = new Scanner(System.in);
        // 入力
        int a = Integer.parseInt(sc.next()); // +1
        int b = Integer.parseInt(sc.next()); // 0
        int c = Integer.parseInt(sc.next()); // -1
        int k = Integer.parseInt(sc.next());

        // 出力
        int num = 0;
        int count = 0;
        outside: {
            for (int i = 0; i < a; i++) {
                if (count == k) {
                    break outside;
                }
                num++;
                count++;
            }
            for (int i = 0; i < b; i++) {
                if (count == k) {
                    break outside;
                }
                count++;
            }
            for (int i = 0; i < c; i++) {
                if (count == k) {
                    break outside;
                }
                num--;
                count++;
            }
        }
        System.out.println(num);
    } 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 num = 0; num 0 int count = 0; count 0 System.out.println(num) System.out.println System.out System System.out num String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 入力
        int a = Integer.parseInt(sc.next()); // +1
        int b = Integer.parseInt(sc.next()); // 0
        int c = Integer.parseInt(sc.next()); // -1
        int k = Integer.parseInt(sc.next());

        // 出力
        int num = 0;
        int count = 0;
        outside: {
            for (int i = 0; i < a; i++) {
                if (count == k) {
                    break outside;
                }
                num++;
                count++;
            }
            for (int i = 0; i < b; i++) {
                if (count == k) {
                    break outside;
                }
                count++;
            }
            for (int i = 0; i < c; i++) {
                if (count == k) {
                    break outside;
                }
                num--;
                count++;
            }
        }
        System.out.println(num);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 入力
        int a = Integer.parseInt(sc.next()); // +1
        int b = Integer.parseInt(sc.next()); // 0
        int c = Integer.parseInt(sc.next()); // -1
        int k = Integer.parseInt(sc.next());

        // 出力
        int num = 0;
        int count = 0;
        outside: {
            for (int i = 0; i < a; i++) {
                if (count == k) {
                    break outside;
                }
                num++;
                count++;
            }
            for (int i = 0; i < b; i++) {
                if (count == k) {
                    break outside;
                }
                count++;
            }
            for (int i = 0; i < c; i++) {
                if (count == k) {
                    break outside;
                }
                num--;
                count++;
            }
        }
        System.out.println(num);
    }
} 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()); // +1
        int b = Integer.parseInt(sc.next()); // 0
        int c = Integer.parseInt(sc.next()); // -1
        int k = Integer.parseInt(sc.next());

        // 出力
        int num = 0;
        int count = 0;
        outside: {
            for (int i = 0; i < a; i++) {
                if (count == k) {
                    break outside;
                }
                num++;
                count++;
            }
            for (int i = 0; i < b; i++) {
                if (count == k) {
                    break outside;
                }
                count++;
            }
            for (int i = 0; i < c; i++) {
                if (count == k) {
                    break outside;
                }
                num--;
                count++;
            }
        }
        System.out.println(num);
    }
}
