// 提出時に消す


import java.util.Scanner;

// 提出時にMainに変更する
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();

        int ans = 0;
        int cnt = 0;
        while (cnt < k) {
            if (0 < a) {
                ans++;
                cnt++;
                a--;
                continue;
            }
            if (0 < b) {
                cnt++;
                b--;
                continue;
            }
            if (0 < c) {
                cnt++;
                ans--;
                continue;
            }
        }

        System.out.println(ans);
        sc.close();
    }
} 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();

        int ans = 0;
        int cnt = 0;
        while (cnt < k) {
            if (0 < a) {
                ans++;
                cnt++;
                a--;
                continue;
            }
            if (0 < b) {
                cnt++;
                b--;
                continue;
            }
            if (0 < c) {
                cnt++;
                ans--;
                continue;
            }
        }

        System.out.println(ans);
        sc.close();
    }
} 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();

        int ans = 0;
        int cnt = 0;
        while (cnt < k) {
            if (0 < a) {
                ans++;
                cnt++;
                a--;
                continue;
            }
            if (0 < b) {
                cnt++;
                b--;
                continue;
            }
            if (0 < c) {
                cnt++;
                ans--;
                continue;
            }
        }

        System.out.println(ans);
        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();

        int ans = 0;
        int cnt = 0;
        while (cnt < k) {
            if (0 < a) {
                ans++;
                cnt++;
                a--;
                continue;
            }
            if (0 < b) {
                cnt++;
                b--;
                continue;
            }
            if (0 < c) {
                cnt++;
                ans--;
                continue;
            }
        }

        System.out.println(ans);
        sc.close();
    } 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 int ans = 0; ans 0 int cnt = 0; cnt 0 while (cnt < k) {
            if (0 < a) {
                ans++;
                cnt++;
                a--;
                continue;
            }
            if (0 < b) {
                cnt++;
                b--;
                continue;
            }
            if (0 < c) {
                cnt++;
                ans--;
                continue;
            }
        } cnt < k cnt k {
            if (0 < a) {
                ans++;
                cnt++;
                a--;
                continue;
            }
            if (0 < b) {
                cnt++;
                b--;
                continue;
            }
            if (0 < c) {
                cnt++;
                ans--;
                continue;
            }
        } if (0 < a) {
                ans++;
                cnt++;
                a--;
                continue;
            } 0 < a 0 a {
                ans++;
                cnt++;
                a--;
                continue;
            } ans++ ans cnt++ cnt a-- a continue; if (0 < b) {
                cnt++;
                b--;
                continue;
            } 0 < b 0 b {
                cnt++;
                b--;
                continue;
            } cnt++ cnt b-- b continue; if (0 < c) {
                cnt++;
                ans--;
                continue;
            } 0 < c 0 c {
                cnt++;
                ans--;
                continue;
            } cnt++ cnt ans-- ans continue; System.out.println(ans) System.out.println System.out System System.out ans sc.close() sc.close sc 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();

        int ans = 0;
        int cnt = 0;
        while (cnt < k) {
            if (0 < a) {
                ans++;
                cnt++;
                a--;
                continue;
            }
            if (0 < b) {
                cnt++;
                b--;
                continue;
            }
            if (0 < c) {
                cnt++;
                ans--;
                continue;
            }
        }

        System.out.println(ans);
        sc.close();
    }
} 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();

        int ans = 0;
        int cnt = 0;
        while (cnt < k) {
            if (0 < a) {
                ans++;
                cnt++;
                a--;
                continue;
            }
            if (0 < b) {
                cnt++;
                b--;
                continue;
            }
            if (0 < c) {
                cnt++;
                ans--;
                continue;
            }
        }

        System.out.println(ans);
        sc.close();
    }
} Main
// 提出時に消す


import java.util.Scanner;

// 提出時にMainに変更する
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();

        int ans = 0;
        int cnt = 0;
        while (cnt < k) {
            if (0 < a) {
                ans++;
                cnt++;
                a--;
                continue;
            }
            if (0 < b) {
                cnt++;
                b--;
                continue;
            }
            if (0 < c) {
                cnt++;
                ans--;
                continue;
            }
        }

        System.out.println(ans);
        sc.close();
    }
}