import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        EasyLinearProgramming solver = new EasyLinearProgramming();
        solver.solve(1, in, out);
        out.close();
    }

    static class EasyLinearProgramming {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            int k = in.nextInt();

            int result = 0;
            if (a > 0) {
                int min = Math.min(a, k);
                k -= min;
                result += min;
            }
            if (b > 0) {
                int min = Math.min(b, k);
                k -= min;
            }
            if (c > 0) {
                int min = Math.min(c, k);
                k -= min;
                result -= min;
            }
            out.append(String.valueOf(result));
        }

    }
}
 import java.io.OutputStream; OutputStream java.io import java.io.IOException; IOException java.io import java.io.InputStream; InputStream java.io import java.io.PrintWriter; PrintWriter java.io import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        EasyLinearProgramming solver = new EasyLinearProgramming();
        solver.solve(1, in, out);
        out.close();
    }

    static class EasyLinearProgramming {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            int k = in.nextInt();

            int result = 0;
            if (a > 0) {
                int min = Math.min(a, k);
                k -= min;
                result += min;
            }
            if (b > 0) {
                int min = Math.min(b, k);
                k -= min;
            }
            if (c > 0) {
                int min = Math.min(c, k);
                k -= min;
                result -= min;
            }
            out.append(String.valueOf(result));
        }

    }
} Main public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        EasyLinearProgramming solver = new EasyLinearProgramming();
        solver.solve(1, in, out);
        out.close();
    } main {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        EasyLinearProgramming solver = new EasyLinearProgramming();
        solver.solve(1, in, out);
        out.close();
    } InputStream inputStream = System.in; inputStream System.in System System.in OutputStream outputStream = System.out; outputStream System.out System System.out Scanner in = new Scanner(inputStream); in new Scanner(inputStream) PrintWriter out = new PrintWriter(outputStream); out new PrintWriter(outputStream) EasyLinearProgramming solver = new EasyLinearProgramming(); solver new EasyLinearProgramming() solver.solve(1, in, out) solver.solve solver 1 in out out.close() out.close out String[] args args static class EasyLinearProgramming {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            int k = in.nextInt();

            int result = 0;
            if (a > 0) {
                int min = Math.min(a, k);
                k -= min;
                result += min;
            }
            if (b > 0) {
                int min = Math.min(b, k);
                k -= min;
            }
            if (c > 0) {
                int min = Math.min(c, k);
                k -= min;
                result -= min;
            }
            out.append(String.valueOf(result));
        }

    } EasyLinearProgramming public void solve(int testNumber, Scanner in, PrintWriter out) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            int k = in.nextInt();

            int result = 0;
            if (a > 0) {
                int min = Math.min(a, k);
                k -= min;
                result += min;
            }
            if (b > 0) {
                int min = Math.min(b, k);
                k -= min;
            }
            if (c > 0) {
                int min = Math.min(c, k);
                k -= min;
                result -= min;
            }
            out.append(String.valueOf(result));
        } solve {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            int k = in.nextInt();

            int result = 0;
            if (a > 0) {
                int min = Math.min(a, k);
                k -= min;
                result += min;
            }
            if (b > 0) {
                int min = Math.min(b, k);
                k -= min;
            }
            if (c > 0) {
                int min = Math.min(c, k);
                k -= min;
                result -= min;
            }
            out.append(String.valueOf(result));
        } int a = in.nextInt(); a in.nextInt() in.nextInt in int b = in.nextInt(); b in.nextInt() in.nextInt in int c = in.nextInt(); c in.nextInt() in.nextInt in int k = in.nextInt(); k in.nextInt() in.nextInt in int result = 0; result 0 if (a > 0) {
                int min = Math.min(a, k);
                k -= min;
                result += min;
            } a > 0 a 0 {
                int min = Math.min(a, k);
                k -= min;
                result += min;
            } int min = Math.min(a, k); min Math.min(a, k) Math.min Math a k k -= min k min result += min result min if (b > 0) {
                int min = Math.min(b, k);
                k -= min;
            } b > 0 b 0 {
                int min = Math.min(b, k);
                k -= min;
            } int min = Math.min(b, k); min Math.min(b, k) Math.min Math b k k -= min k min if (c > 0) {
                int min = Math.min(c, k);
                k -= min;
                result -= min;
            } c > 0 c 0 {
                int min = Math.min(c, k);
                k -= min;
                result -= min;
            } int min = Math.min(c, k); min Math.min(c, k) Math.min Math c k k -= min k min result -= min result min out.append(String.valueOf(result)) out.append out String.valueOf(result) String.valueOf String result int testNumber testNumber Scanner in in PrintWriter out out public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        EasyLinearProgramming solver = new EasyLinearProgramming();
        solver.solve(1, in, out);
        out.close();
    }

    static class EasyLinearProgramming {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            int k = in.nextInt();

            int result = 0;
            if (a > 0) {
                int min = Math.min(a, k);
                k -= min;
                result += min;
            }
            if (b > 0) {
                int min = Math.min(b, k);
                k -= min;
            }
            if (c > 0) {
                int min = Math.min(c, k);
                k -= min;
                result -= min;
            }
            out.append(String.valueOf(result));
        }

    }
} public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        EasyLinearProgramming solver = new EasyLinearProgramming();
        solver.solve(1, in, out);
        out.close();
    }

    static class EasyLinearProgramming {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            int k = in.nextInt();

            int result = 0;
            if (a > 0) {
                int min = Math.min(a, k);
                k -= min;
                result += min;
            }
            if (b > 0) {
                int min = Math.min(b, k);
                k -= min;
            }
            if (c > 0) {
                int min = Math.min(c, k);
                k -= min;
                result -= min;
            }
            out.append(String.valueOf(result));
        }

    }
} Main
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        EasyLinearProgramming solver = new EasyLinearProgramming();
        solver.solve(1, in, out);
        out.close();
    }

    static class EasyLinearProgramming {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            int k = in.nextInt();

            int result = 0;
            if (a > 0) {
                int min = Math.min(a, k);
                k -= min;
                result += min;
            }
            if (b > 0) {
                int min = Math.min(b, k);
                k -= min;
            }
            if (c > 0) {
                int min = Math.min(c, k);
                k -= min;
                result -= min;
            }
            out.append(String.valueOf(result));
        }

    }
}

