
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int a = scn.nextInt();
        int b = scn.nextInt();
        int c = scn.nextInt();
        int k = scn.nextInt();
        int ans = 0;
        for (int i = 0; i < a; i++) {
            ans++;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        for (int i = 0; i < b; i++) {
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        for (int i = 0; i < c; i++) {
            ans--;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        System.out.println(ans);
    }
} import java.util.*; util.* java public class Main {

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int a = scn.nextInt();
        int b = scn.nextInt();
        int c = scn.nextInt();
        int k = scn.nextInt();
        int ans = 0;
        for (int i = 0; i < a; i++) {
            ans++;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        for (int i = 0; i < b; i++) {
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        for (int i = 0; i < c; i++) {
            ans--;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        System.out.println(ans);
    }
} Main public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int a = scn.nextInt();
        int b = scn.nextInt();
        int c = scn.nextInt();
        int k = scn.nextInt();
        int ans = 0;
        for (int i = 0; i < a; i++) {
            ans++;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        for (int i = 0; i < b; i++) {
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        for (int i = 0; i < c; i++) {
            ans--;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        System.out.println(ans);
    } main {
        Scanner scn = new Scanner(System.in);
        int a = scn.nextInt();
        int b = scn.nextInt();
        int c = scn.nextInt();
        int k = scn.nextInt();
        int ans = 0;
        for (int i = 0; i < a; i++) {
            ans++;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        for (int i = 0; i < b; i++) {
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        for (int i = 0; i < c; i++) {
            ans--;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        System.out.println(ans);
    } Scanner scn = new Scanner(System.in); scn new Scanner(System.in) int a = scn.nextInt(); a scn.nextInt() scn.nextInt scn int b = scn.nextInt(); b scn.nextInt() scn.nextInt scn int c = scn.nextInt(); c scn.nextInt() scn.nextInt scn int k = scn.nextInt(); k scn.nextInt() scn.nextInt scn int ans = 0; ans 0 for (int i = 0; i < a; i++) {
            ans++;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        } int i = 0; int i = 0; i 0 i < a i a i++ i++ i {
            ans++;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        } {
            ans++;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        } ans++ ans k-- k if (k <= 0) {
                System.out.println(ans);
                return;
            } k <= 0 k 0 {
                System.out.println(ans);
                return;
            } System.out.println(ans) System.out.println System.out System System.out ans return; for (int i = 0; i < b; i++) {
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        } int i = 0; int i = 0; i 0 i < b i b i++ i++ i {
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        } {
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        } k-- k if (k <= 0) {
                System.out.println(ans);
                return;
            } k <= 0 k 0 {
                System.out.println(ans);
                return;
            } System.out.println(ans) System.out.println System.out System System.out ans return; for (int i = 0; i < c; i++) {
            ans--;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        } int i = 0; int i = 0; i 0 i < c i c i++ i++ i {
            ans--;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        } {
            ans--;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        } ans-- ans k-- k if (k <= 0) {
                System.out.println(ans);
                return;
            } k <= 0 k 0 {
                System.out.println(ans);
                return;
            } System.out.println(ans) System.out.println System.out System System.out ans return; System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int a = scn.nextInt();
        int b = scn.nextInt();
        int c = scn.nextInt();
        int k = scn.nextInt();
        int ans = 0;
        for (int i = 0; i < a; i++) {
            ans++;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        for (int i = 0; i < b; i++) {
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        for (int i = 0; i < c; i++) {
            ans--;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        System.out.println(ans);
    }
} public class Main {

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int a = scn.nextInt();
        int b = scn.nextInt();
        int c = scn.nextInt();
        int k = scn.nextInt();
        int ans = 0;
        for (int i = 0; i < a; i++) {
            ans++;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        for (int i = 0; i < b; i++) {
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        for (int i = 0; i < c; i++) {
            ans--;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        System.out.println(ans);
    }
} Main

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int a = scn.nextInt();
        int b = scn.nextInt();
        int c = scn.nextInt();
        int k = scn.nextInt();
        int ans = 0;
        for (int i = 0; i < a; i++) {
            ans++;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        for (int i = 0; i < b; i++) {
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        for (int i = 0; i < c; i++) {
            ans--;
            k--;
            if (k <= 0) {
                System.out.println(ans);
                return;
            }
        }
        System.out.println(ans);
    }
}
