import java.util.*;
import java.io.*;
import static java.lang.Math.*;

public class Main {
    static StringTokenizer st;
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {
        int a = nextInt();
        int b = nextInt();
        int c = nextInt();
        int k = nextInt();

        int result = 0;
        int sum = 0;
        for (int i = 0; i < a; i++) {
            result += 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }
        for (int i = 0; i < b; i++) {
            result += 0;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }
        for (int i = 0; i < c; i++) {
            result -= 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }

    }

    static char nextChar() throws IOException {
        return next().charAt(0);
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static int[] readArray(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextInt();
        }
        return a;
    }

    static long[] readLongArray(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextLong();
        }
        return a;
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine().trim());
        }
        return st.nextToken();
    }

    static int Char2Int(Character c) {
        return Character.getNumericValue(c);
    }

    static double Char2Double(Character c) {
        return (double) Char2Int(c);
    }

    static int nCr(int n, int r) {
        return fact(n) / (fact(r) * fact(n - r));
    }

    // Returns factorial of n
    static int fact(int n) {
        int res = 1;
        for (int i = 2; i <= n; i++) {
            res = res * i;
        }
        return res;
    }

    static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        }
    }
} import java.util.*; util.* java import java.io.*; io.* java import static java.lang.Math.*; import static java.lang.Math.*; import static java.lang.Math.*; public class Main {
    static StringTokenizer st;
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {
        int a = nextInt();
        int b = nextInt();
        int c = nextInt();
        int k = nextInt();

        int result = 0;
        int sum = 0;
        for (int i = 0; i < a; i++) {
            result += 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }
        for (int i = 0; i < b; i++) {
            result += 0;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }
        for (int i = 0; i < c; i++) {
            result -= 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }

    }

    static char nextChar() throws IOException {
        return next().charAt(0);
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static int[] readArray(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextInt();
        }
        return a;
    }

    static long[] readLongArray(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextLong();
        }
        return a;
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine().trim());
        }
        return st.nextToken();
    }

    static int Char2Int(Character c) {
        return Character.getNumericValue(c);
    }

    static double Char2Double(Character c) {
        return (double) Char2Int(c);
    }

    static int nCr(int n, int r) {
        return fact(n) / (fact(r) * fact(n - r));
    }

    // Returns factorial of n
    static int fact(int n) {
        int res = 1;
        for (int i = 2; i <= n; i++) {
            res = res * i;
        }
        return res;
    }

    static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        }
    }
} Main static StringTokenizer st; st static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) public static void main(String[] args) throws IOException {
        int a = nextInt();
        int b = nextInt();
        int c = nextInt();
        int k = nextInt();

        int result = 0;
        int sum = 0;
        for (int i = 0; i < a; i++) {
            result += 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }
        for (int i = 0; i < b; i++) {
            result += 0;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }
        for (int i = 0; i < c; i++) {
            result -= 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }

    } main {
        int a = nextInt();
        int b = nextInt();
        int c = nextInt();
        int k = nextInt();

        int result = 0;
        int sum = 0;
        for (int i = 0; i < a; i++) {
            result += 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }
        for (int i = 0; i < b; i++) {
            result += 0;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }
        for (int i = 0; i < c; i++) {
            result -= 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }

    } int a = nextInt(); a nextInt() nextInt int b = nextInt(); b nextInt() nextInt int c = nextInt(); c nextInt() nextInt int k = nextInt(); k nextInt() nextInt int result = 0; result 0 int sum = 0; sum 0 for (int i = 0; i < a; i++) {
            result += 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        } int i = 0; int i = 0; i 0 i < a i a i++ i++ i {
            result += 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        } {
            result += 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        } result += 1 result 1 sum += 1 sum 1 if (sum == k) {
                System.out.println(result);
                return;
            } sum == k sum k {
                System.out.println(result);
                return;
            } System.out.println(result) System.out.println System.out System System.out result return; for (int i = 0; i < b; i++) {
            result += 0;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        } int i = 0; int i = 0; i 0 i < b i b i++ i++ i {
            result += 0;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        } {
            result += 0;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        } result += 0 result 0 sum += 1 sum 1 if (sum == k) {
                System.out.println(result);
                return;
            } sum == k sum k {
                System.out.println(result);
                return;
            } System.out.println(result) System.out.println System.out System System.out result return; for (int i = 0; i < c; i++) {
            result -= 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        } int i = 0; int i = 0; i 0 i < c i c i++ i++ i {
            result -= 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        } {
            result -= 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        } result -= 1 result 1 sum += 1 sum 1 if (sum == k) {
                System.out.println(result);
                return;
            } sum == k sum k {
                System.out.println(result);
                return;
            } System.out.println(result) System.out.println System.out System System.out result return; String[] args args static char nextChar() throws IOException {
        return next().charAt(0);
    } nextChar {
        return next().charAt(0);
    } return next().charAt(0); next().charAt(0) next().charAt next() next 0 static int nextInt() throws IOException {
        return Integer.parseInt(next());
    } nextInt {
        return Integer.parseInt(next());
    } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next static long nextLong() throws IOException {
        return Long.parseLong(next());
    } nextLong {
        return Long.parseLong(next());
    } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    } nextDouble {
        return Double.parseDouble(next());
    } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next static int[] readArray(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextInt();
        }
        return a;
    } readArray {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextInt();
        }
        return a;
    } int[] a = new int[n]; a new int[n] n for (int i = 0; i < n; i++) {
            a[i] = nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            a[i] = nextInt();
        } {
            a[i] = nextInt();
        } a[i] = nextInt() a[i] a i nextInt() nextInt return a; a int n n static long[] readLongArray(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextLong();
        }
        return a;
    } readLongArray {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextLong();
        }
        return a;
    } long[] a = new long[n]; a new long[n] n for (int i = 0; i < n; i++) {
            a[i] = nextLong();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            a[i] = nextLong();
        } {
            a[i] = nextLong();
        } a[i] = nextLong() a[i] a i nextLong() nextLong return a; a int n n static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine().trim());
        }
        return st.nextToken();
    } next {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine().trim());
        }
        return st.nextToken();
    } while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine().trim());
        } st == null || !st.hasMoreTokens() st == null st null !st.hasMoreTokens() st.hasMoreTokens() st.hasMoreTokens st {
            st = new StringTokenizer(br.readLine().trim());
        } st = new StringTokenizer(br.readLine().trim()) st new StringTokenizer(br.readLine().trim()) return st.nextToken(); st.nextToken() st.nextToken st static int Char2Int(Character c) {
        return Character.getNumericValue(c);
    } Char2Int {
        return Character.getNumericValue(c);
    } return Character.getNumericValue(c); Character.getNumericValue(c) Character.getNumericValue Character c Character c c static double Char2Double(Character c) {
        return (double) Char2Int(c);
    } Char2Double {
        return (double) Char2Int(c);
    } return (double) Char2Int(c); (double) Char2Int(c) Char2Int c Character c c static int nCr(int n, int r) {
        return fact(n) / (fact(r) * fact(n - r));
    } nCr {
        return fact(n) / (fact(r) * fact(n - r));
    } return fact(n) / (fact(r) * fact(n - r)); fact(n) / (fact(r) * fact(n - r)) fact(n) fact n (fact(r) * fact(n - r)) fact(r) fact r fact(n - r) fact n - r n r int n n int r r // Returns factorial of n
    static int fact(int n) {
        int res = 1;
        for (int i = 2; i <= n; i++) {
            res = res * i;
        }
        return res;
    } fact {
        int res = 1;
        for (int i = 2; i <= n; i++) {
            res = res * i;
        }
        return res;
    } int res = 1; res 1 for (int i = 2; i <= n; i++) {
            res = res * i;
        } int i = 2; int i = 2; i 2 i <= n i n i++ i++ i {
            res = res * i;
        } {
            res = res * i;
        } res = res * i res res * i res i return res; res int n n static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        }
    } gcd {
        if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        }
    } if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        } b == 0 b 0 {
            return a;
        } return a; a {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        } a = a % b a a % b a b return gcd(b, a); // 残り部分から最小の正方形を見つける gcd(b, a) gcd b a int a a int b b public class Main {
    static StringTokenizer st;
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {
        int a = nextInt();
        int b = nextInt();
        int c = nextInt();
        int k = nextInt();

        int result = 0;
        int sum = 0;
        for (int i = 0; i < a; i++) {
            result += 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }
        for (int i = 0; i < b; i++) {
            result += 0;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }
        for (int i = 0; i < c; i++) {
            result -= 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }

    }

    static char nextChar() throws IOException {
        return next().charAt(0);
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static int[] readArray(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextInt();
        }
        return a;
    }

    static long[] readLongArray(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextLong();
        }
        return a;
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine().trim());
        }
        return st.nextToken();
    }

    static int Char2Int(Character c) {
        return Character.getNumericValue(c);
    }

    static double Char2Double(Character c) {
        return (double) Char2Int(c);
    }

    static int nCr(int n, int r) {
        return fact(n) / (fact(r) * fact(n - r));
    }

    // Returns factorial of n
    static int fact(int n) {
        int res = 1;
        for (int i = 2; i <= n; i++) {
            res = res * i;
        }
        return res;
    }

    static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        }
    }
} public class Main {
    static StringTokenizer st;
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {
        int a = nextInt();
        int b = nextInt();
        int c = nextInt();
        int k = nextInt();

        int result = 0;
        int sum = 0;
        for (int i = 0; i < a; i++) {
            result += 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }
        for (int i = 0; i < b; i++) {
            result += 0;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }
        for (int i = 0; i < c; i++) {
            result -= 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }

    }

    static char nextChar() throws IOException {
        return next().charAt(0);
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static int[] readArray(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextInt();
        }
        return a;
    }

    static long[] readLongArray(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextLong();
        }
        return a;
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine().trim());
        }
        return st.nextToken();
    }

    static int Char2Int(Character c) {
        return Character.getNumericValue(c);
    }

    static double Char2Double(Character c) {
        return (double) Char2Int(c);
    }

    static int nCr(int n, int r) {
        return fact(n) / (fact(r) * fact(n - r));
    }

    // Returns factorial of n
    static int fact(int n) {
        int res = 1;
        for (int i = 2; i <= n; i++) {
            res = res * i;
        }
        return res;
    }

    static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        }
    }
} Main
import java.util.*;
import java.io.*;
import static java.lang.Math.*;

public class Main {
    static StringTokenizer st;
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {
        int a = nextInt();
        int b = nextInt();
        int c = nextInt();
        int k = nextInt();

        int result = 0;
        int sum = 0;
        for (int i = 0; i < a; i++) {
            result += 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }
        for (int i = 0; i < b; i++) {
            result += 0;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }
        for (int i = 0; i < c; i++) {
            result -= 1;
            sum += 1;
            if (sum == k) {
                System.out.println(result);
                return;
            }
        }

    }

    static char nextChar() throws IOException {
        return next().charAt(0);
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static int[] readArray(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextInt();
        }
        return a;
    }

    static long[] readLongArray(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextLong();
        }
        return a;
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine().trim());
        }
        return st.nextToken();
    }

    static int Char2Int(Character c) {
        return Character.getNumericValue(c);
    }

    static double Char2Double(Character c) {
        return (double) Char2Int(c);
    }

    static int nCr(int n, int r) {
        return fact(n) / (fact(r) * fact(n - r));
    }

    // Returns factorial of n
    static int fact(int n) {
        int res = 1;
        for (int i = 2; i <= n; i++) {
            res = res * i;
        }
        return res;
    }

    static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        } else {
            a = a % b; // 残り部分
            return gcd(b, a); // 残り部分から最小の正方形を見つける
        }
    }
}