import java.io.*;
import java.math.BigInteger;
import java.util.*;


public class Main {

    static long mod = ((long) (1E9 + 7));

    public static void main(String[] args) {
//        Parser scr = new Parser(System.in);
        FastScanner scr = new FastScanner();
        PrintWriter out = new PrintWriter(System.out, true);
//        int t = scr.nextInt();
//        while (t-- > 0)
        solve(scr, out);
        out.close();
    }


    static void solve(FastScanner in, PrintWriter out) {
//        int N = in.nextInt();
//        int M = in.nextInt();
//        int X = in.nextInt();
//        int[] a = new int[M];
//        for (int i = 0; i < N; i++) {
//            int C = in.nextInt();
//            a[i] = in.nextInt();
//
//        }
        long A = in.nextLong();
        long B = in.nextLong();
        long C = in.nextLong();
        long K = in.nextLong();
        long S = 0;

        if (A <= K) {
            S += A;
            K -= A;

        } else {
            S += K;
            K = 0;
        }
        if (K == 0) {
            out.println(S);
            return;
        }

        if (B <= K) {
            K -= B;
        } else {
            K = 0;
        }
        if (K == 0) {
            out.println(S);
            return;
        }
        if (C <= K) {
            S -= C;
        } else {
            S -= K;
        }
        out.println(S);


    }

    static class Parser {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Parser(InputStream in) {
            din = new DataInputStream(in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String nextString(int maxSize) {
            byte[] ch = new byte[maxSize];
            int point = 0;
            try {
                byte c = read();
                while (c == ' ' || c == '\n' || c == '\r')
                    c = read();
                while (c != ' ' && c != '\n' && c != '\r') {
                    ch[point++] = c;
                    c = read();
                }
            } catch (Exception e) {
            }
            return new String(ch, 0, point);
        }

        public int nextInt() {
            int ret = 0;
            boolean neg;
            try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            }
            return ret;
        }

        public long nextLong() {
            long ret = 0;
            boolean neg;
            try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            }
            return ret;
        }

        private void fillBuffer() {
            try {
                bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            } catch (Exception e) {
            }
            if (bytesRead == -1) buffer[0] = -1;
        }

        private byte read() {
            if (bufferPointer == bytesRead) fillBuffer();
            return buffer[bufferPointer++];
        }
    }


    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            init();
        }

        public FastScanner(String name) {
            init(name);
        }

        public FastScanner(boolean isOnlineJudge) {
            if (!isOnlineJudge || System.getProperty("ONLINE_JUDGE") != null) {
                init();
            } else {
                init("input.txt");
            }
        }

        private void init() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        private void init(String name) {
            try {
                br = new BufferedReader(new FileReader(name));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }


        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }

    }


} import java.io.*; io.* java import java.math.BigInteger; BigInteger java.math import java.util.*; util.* java public class Main {

    static long mod = ((long) (1E9 + 7));

    public static void main(String[] args) {
//        Parser scr = new Parser(System.in);
        FastScanner scr = new FastScanner();
        PrintWriter out = new PrintWriter(System.out, true);
//        int t = scr.nextInt();
//        while (t-- > 0)
        solve(scr, out);
        out.close();
    }


    static void solve(FastScanner in, PrintWriter out) {
//        int N = in.nextInt();
//        int M = in.nextInt();
//        int X = in.nextInt();
//        int[] a = new int[M];
//        for (int i = 0; i < N; i++) {
//            int C = in.nextInt();
//            a[i] = in.nextInt();
//
//        }
        long A = in.nextLong();
        long B = in.nextLong();
        long C = in.nextLong();
        long K = in.nextLong();
        long S = 0;

        if (A <= K) {
            S += A;
            K -= A;

        } else {
            S += K;
            K = 0;
        }
        if (K == 0) {
            out.println(S);
            return;
        }

        if (B <= K) {
            K -= B;
        } else {
            K = 0;
        }
        if (K == 0) {
            out.println(S);
            return;
        }
        if (C <= K) {
            S -= C;
        } else {
            S -= K;
        }
        out.println(S);


    }

    static class Parser {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Parser(InputStream in) {
            din = new DataInputStream(in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String nextString(int maxSize) {
            byte[] ch = new byte[maxSize];
            int point = 0;
            try {
                byte c = read();
                while (c == ' ' || c == '\n' || c == '\r')
                    c = read();
                while (c != ' ' && c != '\n' && c != '\r') {
                    ch[point++] = c;
                    c = read();
                }
            } catch (Exception e) {
            }
            return new String(ch, 0, point);
        }

        public int nextInt() {
            int ret = 0;
            boolean neg;
            try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            }
            return ret;
        }

        public long nextLong() {
            long ret = 0;
            boolean neg;
            try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            }
            return ret;
        }

        private void fillBuffer() {
            try {
                bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            } catch (Exception e) {
            }
            if (bytesRead == -1) buffer[0] = -1;
        }

        private byte read() {
            if (bufferPointer == bytesRead) fillBuffer();
            return buffer[bufferPointer++];
        }
    }


    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            init();
        }

        public FastScanner(String name) {
            init(name);
        }

        public FastScanner(boolean isOnlineJudge) {
            if (!isOnlineJudge || System.getProperty("ONLINE_JUDGE") != null) {
                init();
            } else {
                init("input.txt");
            }
        }

        private void init() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        private void init(String name) {
            try {
                br = new BufferedReader(new FileReader(name));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }


        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }

    }


} Main static long mod = ((long) (1E9 + 7)); mod ((long) (1E9 + 7)) 1E9 7 public static void main(String[] args) {
//        Parser scr = new Parser(System.in);
        FastScanner scr = new FastScanner();
        PrintWriter out = new PrintWriter(System.out, true);
//        int t = scr.nextInt();
//        while (t-- > 0)
        solve(scr, out);
        out.close();
    } main {
//        Parser scr = new Parser(System.in);
        FastScanner scr = new FastScanner();
        PrintWriter out = new PrintWriter(System.out, true);
//        int t = scr.nextInt();
//        while (t-- > 0)
        solve(scr, out);
        out.close();
    } FastScanner scr = new FastScanner(); scr new FastScanner() PrintWriter out = new PrintWriter(System.out, true); out new PrintWriter(System.out, true) solve(scr, out) solve scr out out.close() out.close out String[] args args static void solve(FastScanner in, PrintWriter out) {
//        int N = in.nextInt();
//        int M = in.nextInt();
//        int X = in.nextInt();
//        int[] a = new int[M];
//        for (int i = 0; i < N; i++) {
//            int C = in.nextInt();
//            a[i] = in.nextInt();
//
//        }
        long A = in.nextLong();
        long B = in.nextLong();
        long C = in.nextLong();
        long K = in.nextLong();
        long S = 0;

        if (A <= K) {
            S += A;
            K -= A;

        } else {
            S += K;
            K = 0;
        }
        if (K == 0) {
            out.println(S);
            return;
        }

        if (B <= K) {
            K -= B;
        } else {
            K = 0;
        }
        if (K == 0) {
            out.println(S);
            return;
        }
        if (C <= K) {
            S -= C;
        } else {
            S -= K;
        }
        out.println(S);


    } solve {
//        int N = in.nextInt();
//        int M = in.nextInt();
//        int X = in.nextInt();
//        int[] a = new int[M];
//        for (int i = 0; i < N; i++) {
//            int C = in.nextInt();
//            a[i] = in.nextInt();
//
//        }
        long A = in.nextLong();
        long B = in.nextLong();
        long C = in.nextLong();
        long K = in.nextLong();
        long S = 0;

        if (A <= K) {
            S += A;
            K -= A;

        } else {
            S += K;
            K = 0;
        }
        if (K == 0) {
            out.println(S);
            return;
        }

        if (B <= K) {
            K -= B;
        } else {
            K = 0;
        }
        if (K == 0) {
            out.println(S);
            return;
        }
        if (C <= K) {
            S -= C;
        } else {
            S -= K;
        }
        out.println(S);


    } long A = in.nextLong(); A in.nextLong() in.nextLong in long B = in.nextLong(); B in.nextLong() in.nextLong in long C = in.nextLong(); C in.nextLong() in.nextLong in long K = in.nextLong(); K in.nextLong() in.nextLong in long S = 0; S 0 if (A <= K) {
            S += A;
            K -= A;

        } else {
            S += K;
            K = 0;
        } A <= K A K {
            S += A;
            K -= A;

        } S += A S A K -= A K A {
            S += K;
            K = 0;
        } S += K S K K = 0 K 0 if (K == 0) {
            out.println(S);
            return;
        } K == 0 K 0 {
            out.println(S);
            return;
        } out.println(S) out.println out S return; if (B <= K) {
            K -= B;
        } else {
            K = 0;
        } B <= K B K {
            K -= B;
        } K -= B K B {
            K = 0;
        } K = 0 K 0 if (K == 0) {
            out.println(S);
            return;
        } K == 0 K 0 {
            out.println(S);
            return;
        } out.println(S) out.println out S return; if (C <= K) {
            S -= C;
        } else {
            S -= K;
        } C <= K C K {
            S -= C;
        } S -= C S C {
            S -= K;
        } S -= K S K out.println(S) out.println out S FastScanner in in PrintWriter out out static class Parser {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Parser(InputStream in) {
            din = new DataInputStream(in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String nextString(int maxSize) {
            byte[] ch = new byte[maxSize];
            int point = 0;
            try {
                byte c = read();
                while (c == ' ' || c == '\n' || c == '\r')
                    c = read();
                while (c != ' ' && c != '\n' && c != '\r') {
                    ch[point++] = c;
                    c = read();
                }
            } catch (Exception e) {
            }
            return new String(ch, 0, point);
        }

        public int nextInt() {
            int ret = 0;
            boolean neg;
            try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            }
            return ret;
        }

        public long nextLong() {
            long ret = 0;
            boolean neg;
            try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            }
            return ret;
        }

        private void fillBuffer() {
            try {
                bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            } catch (Exception e) {
            }
            if (bytesRead == -1) buffer[0] = -1;
        }

        private byte read() {
            if (bufferPointer == bytesRead) fillBuffer();
            return buffer[bufferPointer++];
        }
    } Parser final private int BUFFER_SIZE = 1 << 16; BUFFER_SIZE 1 << 16 1 16 private DataInputStream din; din private byte[] buffer; buffer private int bufferPointer bufferPointer bytesRead; bytesRead public Parser(InputStream in) {
            din = new DataInputStream(in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        } Parser {
            din = new DataInputStream(in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        } din = new DataInputStream(in) din new DataInputStream(in) buffer = new byte[BUFFER_SIZE] buffer new byte[BUFFER_SIZE] BUFFER_SIZE bufferPointer = bytesRead = 0 bufferPointer bytesRead = 0 bytesRead 0 InputStream in in public String nextString(int maxSize) {
            byte[] ch = new byte[maxSize];
            int point = 0;
            try {
                byte c = read();
                while (c == ' ' || c == '\n' || c == '\r')
                    c = read();
                while (c != ' ' && c != '\n' && c != '\r') {
                    ch[point++] = c;
                    c = read();
                }
            } catch (Exception e) {
            }
            return new String(ch, 0, point);
        } nextString {
            byte[] ch = new byte[maxSize];
            int point = 0;
            try {
                byte c = read();
                while (c == ' ' || c == '\n' || c == '\r')
                    c = read();
                while (c != ' ' && c != '\n' && c != '\r') {
                    ch[point++] = c;
                    c = read();
                }
            } catch (Exception e) {
            }
            return new String(ch, 0, point);
        } byte[] ch = new byte[maxSize]; ch new byte[maxSize] maxSize int point = 0; point 0 try {
                byte c = read();
                while (c == ' ' || c == '\n' || c == '\r')
                    c = read();
                while (c != ' ' && c != '\n' && c != '\r') {
                    ch[point++] = c;
                    c = read();
                }
            } catch (Exception e) {
            } catch (Exception e) {
            } Exception e {
            } {
                byte c = read();
                while (c == ' ' || c == '\n' || c == '\r')
                    c = read();
                while (c != ' ' && c != '\n' && c != '\r') {
                    ch[point++] = c;
                    c = read();
                }
            } byte c = read(); c read() read while (c == ' ' || c == '\n' || c == '\r')
                    c = read(); c == ' ' || c == '\n' || c == '\r' c == ' ' || c == '\n' || c == '\r' c == ' ' c ' ' c == '\n' c '\n' c == '\r' c '\r' c = read() c read() read while (c != ' ' && c != '\n' && c != '\r') {
                    ch[point++] = c;
                    c = read();
                } c != ' ' && c != '\n' && c != '\r' c != ' ' && c != '\n' && c != '\r' c != ' ' c ' ' c != '\n' c '\n' c != '\r' c '\r' {
                    ch[point++] = c;
                    c = read();
                } ch[point++] = c ch[point++] ch point++ point c c = read() c read() read return new String(ch, 0, point); new String(ch, 0, point) int maxSize maxSize public int nextInt() {
            int ret = 0;
            boolean neg;
            try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            }
            return ret;
        } nextInt {
            int ret = 0;
            boolean neg;
            try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            }
            return ret;
        } int ret = 0; ret 0 boolean neg; neg try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            } catch (Exception e) {
            } Exception e {
            } {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } byte c = read(); c read() read while (c <= ' ')
                    c = read(); c <= ' ' c ' ' c = read() c read() read neg = c == '-' neg c == '-' c '-' if (neg)
                    c = read(); neg c = read() c read() read do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' '); c > ' ' c ' ' {
                    ret = ret * 10 + c - '0';
                    c = read();
                } ret = ret * 10 + c - '0' ret ret * 10 + c - '0' ret * 10 + c ret * 10 ret 10 c '0' c = read() c read() read if (neg) return -ret; neg return -ret; -ret ret return ret; ret public long nextLong() {
            long ret = 0;
            boolean neg;
            try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            }
            return ret;
        } nextLong {
            long ret = 0;
            boolean neg;
            try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            }
            return ret;
        } long ret = 0; ret 0 boolean neg; neg try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            } catch (Exception e) {
            } Exception e {
            } {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } byte c = read(); c read() read while (c <= ' ')
                    c = read(); c <= ' ' c ' ' c = read() c read() read neg = c == '-' neg c == '-' c '-' if (neg)
                    c = read(); neg c = read() c read() read do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' '); c > ' ' c ' ' {
                    ret = ret * 10 + c - '0';
                    c = read();
                } ret = ret * 10 + c - '0' ret ret * 10 + c - '0' ret * 10 + c ret * 10 ret 10 c '0' c = read() c read() read if (neg) return -ret; neg return -ret; -ret ret return ret; ret private void fillBuffer() {
            try {
                bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            } catch (Exception e) {
            }
            if (bytesRead == -1) buffer[0] = -1;
        } fillBuffer {
            try {
                bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            } catch (Exception e) {
            }
            if (bytesRead == -1) buffer[0] = -1;
        } try {
                bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            } catch (Exception e) {
            } catch (Exception e) {
            } Exception e {
            } {
                bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            } bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE) bytesRead din.read(buffer, bufferPointer = 0, BUFFER_SIZE) din.read din buffer bufferPointer = 0 bufferPointer 0 BUFFER_SIZE if (bytesRead == -1) buffer[0] = -1; bytesRead == -1 bytesRead -1 1 buffer[0] = -1 buffer[0] buffer 0 -1 1 private byte read() {
            if (bufferPointer == bytesRead) fillBuffer();
            return buffer[bufferPointer++];
        } read {
            if (bufferPointer == bytesRead) fillBuffer();
            return buffer[bufferPointer++];
        } if (bufferPointer == bytesRead) fillBuffer(); bufferPointer == bytesRead bufferPointer bytesRead fillBuffer() fillBuffer return buffer[bufferPointer++]; buffer[bufferPointer++] buffer bufferPointer++ bufferPointer static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            init();
        }

        public FastScanner(String name) {
            init(name);
        }

        public FastScanner(boolean isOnlineJudge) {
            if (!isOnlineJudge || System.getProperty("ONLINE_JUDGE") != null) {
                init();
            } else {
                init("input.txt");
            }
        }

        private void init() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        private void init(String name) {
            try {
                br = new BufferedReader(new FileReader(name));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }


        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }

    } FastScanner BufferedReader br; br StringTokenizer st; st public FastScanner() {
            init();
        } FastScanner {
            init();
        } init() init public FastScanner(String name) {
            init(name);
        } FastScanner {
            init(name);
        } init(name) init name String name name public FastScanner(boolean isOnlineJudge) {
            if (!isOnlineJudge || System.getProperty("ONLINE_JUDGE") != null) {
                init();
            } else {
                init("input.txt");
            }
        } FastScanner {
            if (!isOnlineJudge || System.getProperty("ONLINE_JUDGE") != null) {
                init();
            } else {
                init("input.txt");
            }
        } if (!isOnlineJudge || System.getProperty("ONLINE_JUDGE") != null) {
                init();
            } else {
                init("input.txt");
            } !isOnlineJudge || System.getProperty("ONLINE_JUDGE") != null !isOnlineJudge isOnlineJudge System.getProperty("ONLINE_JUDGE") != null System.getProperty("ONLINE_JUDGE") System.getProperty System "ONLINE_JUDGE" null {
                init();
            } init() init {
                init("input.txt");
            } init("input.txt") init "input.txt" boolean isOnlineJudge isOnlineJudge private void init() {
            br = new BufferedReader(new InputStreamReader(System.in));
        } init {
            br = new BufferedReader(new InputStreamReader(System.in));
        } br = new BufferedReader(new InputStreamReader(System.in)) br new BufferedReader(new InputStreamReader(System.in)) private void init(String name) {
            try {
                br = new BufferedReader(new FileReader(name));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } init {
            try {
                br = new BufferedReader(new FileReader(name));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } try {
                br = new BufferedReader(new FileReader(name));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } FileNotFoundException e {
                e.printStackTrace();
            } e.printStackTrace() e.printStackTrace e {
                br = new BufferedReader(new FileReader(name));
            } br = new BufferedReader(new FileReader(name)) br new BufferedReader(new FileReader(name)) String name name public String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        } nextToken {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        } while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } st == null || !st.hasMoreElements() st == null st null !st.hasMoreElements() st.hasMoreElements() st.hasMoreElements st {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } IOException e {
                    e.printStackTrace();
                } e.printStackTrace() e.printStackTrace e {
                    st = new StringTokenizer(br.readLine());
                } st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st public int nextInt() {
            return Integer.parseInt(nextToken());
        } nextInt {
            return Integer.parseInt(nextToken());
        } return Integer.parseInt(nextToken()); Integer.parseInt(nextToken()) Integer.parseInt Integer nextToken() nextToken public long nextLong() {
            return Long.parseLong(nextToken());
        } nextLong {
            return Long.parseLong(nextToken());
        } return Long.parseLong(nextToken()); Long.parseLong(nextToken()) Long.parseLong Long nextToken() nextToken public double nextDouble() {
            return Double.parseDouble(nextToken());
        } nextDouble {
            return Double.parseDouble(nextToken());
        } return Double.parseDouble(nextToken()); Double.parseDouble(nextToken()) Double.parseDouble Double nextToken() nextToken public class Main {

    static long mod = ((long) (1E9 + 7));

    public static void main(String[] args) {
//        Parser scr = new Parser(System.in);
        FastScanner scr = new FastScanner();
        PrintWriter out = new PrintWriter(System.out, true);
//        int t = scr.nextInt();
//        while (t-- > 0)
        solve(scr, out);
        out.close();
    }


    static void solve(FastScanner in, PrintWriter out) {
//        int N = in.nextInt();
//        int M = in.nextInt();
//        int X = in.nextInt();
//        int[] a = new int[M];
//        for (int i = 0; i < N; i++) {
//            int C = in.nextInt();
//            a[i] = in.nextInt();
//
//        }
        long A = in.nextLong();
        long B = in.nextLong();
        long C = in.nextLong();
        long K = in.nextLong();
        long S = 0;

        if (A <= K) {
            S += A;
            K -= A;

        } else {
            S += K;
            K = 0;
        }
        if (K == 0) {
            out.println(S);
            return;
        }

        if (B <= K) {
            K -= B;
        } else {
            K = 0;
        }
        if (K == 0) {
            out.println(S);
            return;
        }
        if (C <= K) {
            S -= C;
        } else {
            S -= K;
        }
        out.println(S);


    }

    static class Parser {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Parser(InputStream in) {
            din = new DataInputStream(in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String nextString(int maxSize) {
            byte[] ch = new byte[maxSize];
            int point = 0;
            try {
                byte c = read();
                while (c == ' ' || c == '\n' || c == '\r')
                    c = read();
                while (c != ' ' && c != '\n' && c != '\r') {
                    ch[point++] = c;
                    c = read();
                }
            } catch (Exception e) {
            }
            return new String(ch, 0, point);
        }

        public int nextInt() {
            int ret = 0;
            boolean neg;
            try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            }
            return ret;
        }

        public long nextLong() {
            long ret = 0;
            boolean neg;
            try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            }
            return ret;
        }

        private void fillBuffer() {
            try {
                bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            } catch (Exception e) {
            }
            if (bytesRead == -1) buffer[0] = -1;
        }

        private byte read() {
            if (bufferPointer == bytesRead) fillBuffer();
            return buffer[bufferPointer++];
        }
    }


    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            init();
        }

        public FastScanner(String name) {
            init(name);
        }

        public FastScanner(boolean isOnlineJudge) {
            if (!isOnlineJudge || System.getProperty("ONLINE_JUDGE") != null) {
                init();
            } else {
                init("input.txt");
            }
        }

        private void init() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        private void init(String name) {
            try {
                br = new BufferedReader(new FileReader(name));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }


        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }

    }


} public class Main {

    static long mod = ((long) (1E9 + 7));

    public static void main(String[] args) {
//        Parser scr = new Parser(System.in);
        FastScanner scr = new FastScanner();
        PrintWriter out = new PrintWriter(System.out, true);
//        int t = scr.nextInt();
//        while (t-- > 0)
        solve(scr, out);
        out.close();
    }


    static void solve(FastScanner in, PrintWriter out) {
//        int N = in.nextInt();
//        int M = in.nextInt();
//        int X = in.nextInt();
//        int[] a = new int[M];
//        for (int i = 0; i < N; i++) {
//            int C = in.nextInt();
//            a[i] = in.nextInt();
//
//        }
        long A = in.nextLong();
        long B = in.nextLong();
        long C = in.nextLong();
        long K = in.nextLong();
        long S = 0;

        if (A <= K) {
            S += A;
            K -= A;

        } else {
            S += K;
            K = 0;
        }
        if (K == 0) {
            out.println(S);
            return;
        }

        if (B <= K) {
            K -= B;
        } else {
            K = 0;
        }
        if (K == 0) {
            out.println(S);
            return;
        }
        if (C <= K) {
            S -= C;
        } else {
            S -= K;
        }
        out.println(S);


    }

    static class Parser {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Parser(InputStream in) {
            din = new DataInputStream(in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String nextString(int maxSize) {
            byte[] ch = new byte[maxSize];
            int point = 0;
            try {
                byte c = read();
                while (c == ' ' || c == '\n' || c == '\r')
                    c = read();
                while (c != ' ' && c != '\n' && c != '\r') {
                    ch[point++] = c;
                    c = read();
                }
            } catch (Exception e) {
            }
            return new String(ch, 0, point);
        }

        public int nextInt() {
            int ret = 0;
            boolean neg;
            try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            }
            return ret;
        }

        public long nextLong() {
            long ret = 0;
            boolean neg;
            try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            }
            return ret;
        }

        private void fillBuffer() {
            try {
                bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            } catch (Exception e) {
            }
            if (bytesRead == -1) buffer[0] = -1;
        }

        private byte read() {
            if (bufferPointer == bytesRead) fillBuffer();
            return buffer[bufferPointer++];
        }
    }


    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            init();
        }

        public FastScanner(String name) {
            init(name);
        }

        public FastScanner(boolean isOnlineJudge) {
            if (!isOnlineJudge || System.getProperty("ONLINE_JUDGE") != null) {
                init();
            } else {
                init("input.txt");
            }
        }

        private void init() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        private void init(String name) {
            try {
                br = new BufferedReader(new FileReader(name));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }


        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }

    }


} Main
import java.io.*;
import java.math.BigInteger;
import java.util.*;


public class Main {

    static long mod = ((long) (1E9 + 7));

    public static void main(String[] args) {
//        Parser scr = new Parser(System.in);
        FastScanner scr = new FastScanner();
        PrintWriter out = new PrintWriter(System.out, true);
//        int t = scr.nextInt();
//        while (t-- > 0)
        solve(scr, out);
        out.close();
    }


    static void solve(FastScanner in, PrintWriter out) {
//        int N = in.nextInt();
//        int M = in.nextInt();
//        int X = in.nextInt();
//        int[] a = new int[M];
//        for (int i = 0; i < N; i++) {
//            int C = in.nextInt();
//            a[i] = in.nextInt();
//
//        }
        long A = in.nextLong();
        long B = in.nextLong();
        long C = in.nextLong();
        long K = in.nextLong();
        long S = 0;

        if (A <= K) {
            S += A;
            K -= A;

        } else {
            S += K;
            K = 0;
        }
        if (K == 0) {
            out.println(S);
            return;
        }

        if (B <= K) {
            K -= B;
        } else {
            K = 0;
        }
        if (K == 0) {
            out.println(S);
            return;
        }
        if (C <= K) {
            S -= C;
        } else {
            S -= K;
        }
        out.println(S);


    }

    static class Parser {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Parser(InputStream in) {
            din = new DataInputStream(in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String nextString(int maxSize) {
            byte[] ch = new byte[maxSize];
            int point = 0;
            try {
                byte c = read();
                while (c == ' ' || c == '\n' || c == '\r')
                    c = read();
                while (c != ' ' && c != '\n' && c != '\r') {
                    ch[point++] = c;
                    c = read();
                }
            } catch (Exception e) {
            }
            return new String(ch, 0, point);
        }

        public int nextInt() {
            int ret = 0;
            boolean neg;
            try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            }
            return ret;
        }

        public long nextLong() {
            long ret = 0;
            boolean neg;
            try {
                byte c = read();
                while (c <= ' ')
                    c = read();
                neg = c == '-';
                if (neg)
                    c = read();
                do {
                    ret = ret * 10 + c - '0';
                    c = read();
                } while (c > ' ');

                if (neg) return -ret;
            } catch (Exception e) {
            }
            return ret;
        }

        private void fillBuffer() {
            try {
                bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            } catch (Exception e) {
            }
            if (bytesRead == -1) buffer[0] = -1;
        }

        private byte read() {
            if (bufferPointer == bytesRead) fillBuffer();
            return buffer[bufferPointer++];
        }
    }


    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            init();
        }

        public FastScanner(String name) {
            init(name);
        }

        public FastScanner(boolean isOnlineJudge) {
            if (!isOnlineJudge || System.getProperty("ONLINE_JUDGE") != null) {
                init();
            } else {
                init("input.txt");
            }
        }

        private void init() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        private void init(String name) {
            try {
                br = new BufferedReader(new FileReader(name));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextToken());
        }


        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }

    }


}
