import java.io.*;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        InputReader in=new InputReader(System.in);
        int a=in.nextInt();
        int b=in.nextInt();
        int c=in.nextInt();
        int k=in.nextInt();
        if(a>k)
            out.println(k);
        else{
            int res=0;
            res+=a;
            k-=a;
            if(b>k) {
                res += k;
                k=0;
            }
            else
                k-=b;
            res+=-k;
            out.println(res);
        }
        out.flush();
    }
    static class InputReader{
        private BufferedReader in;
        private StringTokenizer tokenizer;
        public InputReader(InputStream stream){
            in = new BufferedReader(new InputStreamReader(stream),32768);
            tokenizer = null;
        }
        public String next() throws IOException{
            while(tokenizer==null || !tokenizer.hasMoreTokens()){
                tokenizer = new StringTokenizer(in.readLine());
            }
            return tokenizer.nextToken();
        }
        public Long nextLong() throws IOException {
            String next = next();
            return Long.valueOf(next);
        }
        public int nextInt() throws IOException{
            return Integer.valueOf(next());
        }
    }
} import java.io.*; io.* java import java.util.StringTokenizer; StringTokenizer java.util public class Main {
    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        InputReader in=new InputReader(System.in);
        int a=in.nextInt();
        int b=in.nextInt();
        int c=in.nextInt();
        int k=in.nextInt();
        if(a>k)
            out.println(k);
        else{
            int res=0;
            res+=a;
            k-=a;
            if(b>k) {
                res += k;
                k=0;
            }
            else
                k-=b;
            res+=-k;
            out.println(res);
        }
        out.flush();
    }
    static class InputReader{
        private BufferedReader in;
        private StringTokenizer tokenizer;
        public InputReader(InputStream stream){
            in = new BufferedReader(new InputStreamReader(stream),32768);
            tokenizer = null;
        }
        public String next() throws IOException{
            while(tokenizer==null || !tokenizer.hasMoreTokens()){
                tokenizer = new StringTokenizer(in.readLine());
            }
            return tokenizer.nextToken();
        }
        public Long nextLong() throws IOException {
            String next = next();
            return Long.valueOf(next);
        }
        public int nextInt() throws IOException{
            return Integer.valueOf(next());
        }
    }
} Main public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        InputReader in=new InputReader(System.in);
        int a=in.nextInt();
        int b=in.nextInt();
        int c=in.nextInt();
        int k=in.nextInt();
        if(a>k)
            out.println(k);
        else{
            int res=0;
            res+=a;
            k-=a;
            if(b>k) {
                res += k;
                k=0;
            }
            else
                k-=b;
            res+=-k;
            out.println(res);
        }
        out.flush();
    } main {
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        InputReader in=new InputReader(System.in);
        int a=in.nextInt();
        int b=in.nextInt();
        int c=in.nextInt();
        int k=in.nextInt();
        if(a>k)
            out.println(k);
        else{
            int res=0;
            res+=a;
            k-=a;
            if(b>k) {
                res += k;
                k=0;
            }
            else
                k-=b;
            res+=-k;
            out.println(res);
        }
        out.flush();
    } PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); out new PrintWriter(new OutputStreamWriter(System.out)) InputReader in=new InputReader(System.in); in new InputReader(System.in) 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 if(a>k)
            out.println(k);
        else{
            int res=0;
            res+=a;
            k-=a;
            if(b>k) {
                res += k;
                k=0;
            }
            else
                k-=b;
            res+=-k;
            out.println(res);
        } a>k a k out.println(k) out.println out k {
            int res=0;
            res+=a;
            k-=a;
            if(b>k) {
                res += k;
                k=0;
            }
            else
                k-=b;
            res+=-k;
            out.println(res);
        } int res=0; res 0 res+=a res a k-=a k a if(b>k) {
                res += k;
                k=0;
            }
            else
                k-=b; b>k b k {
                res += k;
                k=0;
            } res += k res k k=0 k 0 k-=b k b res+=-k res -k k out.println(res) out.println out res out.flush() out.flush out String[] args args static class InputReader{
        private BufferedReader in;
        private StringTokenizer tokenizer;
        public InputReader(InputStream stream){
            in = new BufferedReader(new InputStreamReader(stream),32768);
            tokenizer = null;
        }
        public String next() throws IOException{
            while(tokenizer==null || !tokenizer.hasMoreTokens()){
                tokenizer = new StringTokenizer(in.readLine());
            }
            return tokenizer.nextToken();
        }
        public Long nextLong() throws IOException {
            String next = next();
            return Long.valueOf(next);
        }
        public int nextInt() throws IOException{
            return Integer.valueOf(next());
        }
    } InputReader private BufferedReader in; in private StringTokenizer tokenizer; tokenizer public InputReader(InputStream stream){
            in = new BufferedReader(new InputStreamReader(stream),32768);
            tokenizer = null;
        } InputReader {
            in = new BufferedReader(new InputStreamReader(stream),32768);
            tokenizer = null;
        } in = new BufferedReader(new InputStreamReader(stream),32768) in new BufferedReader(new InputStreamReader(stream),32768) tokenizer = null tokenizer null InputStream stream stream public String next() throws IOException{
            while(tokenizer==null || !tokenizer.hasMoreTokens()){
                tokenizer = new StringTokenizer(in.readLine());
            }
            return tokenizer.nextToken();
        } next {
            while(tokenizer==null || !tokenizer.hasMoreTokens()){
                tokenizer = new StringTokenizer(in.readLine());
            }
            return tokenizer.nextToken();
        } while(tokenizer==null || !tokenizer.hasMoreTokens()){
                tokenizer = new StringTokenizer(in.readLine());
            } tokenizer==null || !tokenizer.hasMoreTokens() tokenizer==null tokenizer null !tokenizer.hasMoreTokens() tokenizer.hasMoreTokens() tokenizer.hasMoreTokens tokenizer {
                tokenizer = new StringTokenizer(in.readLine());
            } tokenizer = new StringTokenizer(in.readLine()) tokenizer new StringTokenizer(in.readLine()) return tokenizer.nextToken(); tokenizer.nextToken() tokenizer.nextToken tokenizer public Long nextLong() throws IOException {
            String next = next();
            return Long.valueOf(next);
        } nextLong {
            String next = next();
            return Long.valueOf(next);
        } String next = next(); next next() next return Long.valueOf(next); Long.valueOf(next) Long.valueOf Long next public int nextInt() throws IOException{
            return Integer.valueOf(next());
        } nextInt {
            return Integer.valueOf(next());
        } return Integer.valueOf(next()); Integer.valueOf(next()) Integer.valueOf Integer next() next public class Main {
    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        InputReader in=new InputReader(System.in);
        int a=in.nextInt();
        int b=in.nextInt();
        int c=in.nextInt();
        int k=in.nextInt();
        if(a>k)
            out.println(k);
        else{
            int res=0;
            res+=a;
            k-=a;
            if(b>k) {
                res += k;
                k=0;
            }
            else
                k-=b;
            res+=-k;
            out.println(res);
        }
        out.flush();
    }
    static class InputReader{
        private BufferedReader in;
        private StringTokenizer tokenizer;
        public InputReader(InputStream stream){
            in = new BufferedReader(new InputStreamReader(stream),32768);
            tokenizer = null;
        }
        public String next() throws IOException{
            while(tokenizer==null || !tokenizer.hasMoreTokens()){
                tokenizer = new StringTokenizer(in.readLine());
            }
            return tokenizer.nextToken();
        }
        public Long nextLong() throws IOException {
            String next = next();
            return Long.valueOf(next);
        }
        public int nextInt() throws IOException{
            return Integer.valueOf(next());
        }
    }
} public class Main {
    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        InputReader in=new InputReader(System.in);
        int a=in.nextInt();
        int b=in.nextInt();
        int c=in.nextInt();
        int k=in.nextInt();
        if(a>k)
            out.println(k);
        else{
            int res=0;
            res+=a;
            k-=a;
            if(b>k) {
                res += k;
                k=0;
            }
            else
                k-=b;
            res+=-k;
            out.println(res);
        }
        out.flush();
    }
    static class InputReader{
        private BufferedReader in;
        private StringTokenizer tokenizer;
        public InputReader(InputStream stream){
            in = new BufferedReader(new InputStreamReader(stream),32768);
            tokenizer = null;
        }
        public String next() throws IOException{
            while(tokenizer==null || !tokenizer.hasMoreTokens()){
                tokenizer = new StringTokenizer(in.readLine());
            }
            return tokenizer.nextToken();
        }
        public Long nextLong() throws IOException {
            String next = next();
            return Long.valueOf(next);
        }
        public int nextInt() throws IOException{
            return Integer.valueOf(next());
        }
    }
} Main
import java.io.*;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        InputReader in=new InputReader(System.in);
        int a=in.nextInt();
        int b=in.nextInt();
        int c=in.nextInt();
        int k=in.nextInt();
        if(a>k)
            out.println(k);
        else{
            int res=0;
            res+=a;
            k-=a;
            if(b>k) {
                res += k;
                k=0;
            }
            else
                k-=b;
            res+=-k;
            out.println(res);
        }
        out.flush();
    }
    static class InputReader{
        private BufferedReader in;
        private StringTokenizer tokenizer;
        public InputReader(InputStream stream){
            in = new BufferedReader(new InputStreamReader(stream),32768);
            tokenizer = null;
        }
        public String next() throws IOException{
            while(tokenizer==null || !tokenizer.hasMoreTokens()){
                tokenizer = new StringTokenizer(in.readLine());
            }
            return tokenizer.nextToken();
        }
        public Long nextLong() throws IOException {
            String next = next();
            return Long.valueOf(next);
        }
        public int nextInt() throws IOException{
            return Integer.valueOf(next());
        }
    }
}