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

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

    static class Task {
        public void solve(InputReader in, PrintWriter out) {
           long a  = in.nextLong();
           long b = in.nextLong();
           long c = in.nextLong();
           long k = in.nextLong();
            long resultSum = 0;
           if(k > 0){
               if(k > a){
                   resultSum += a;
                   k -= a;
               }
               else{
                   resultSum += k;
                   k = 0;
               }
           }
           if(k > 0){
               k -= (k  > b)? b : k;
           }

           if(k > 0){
            if(k > c){
                resultSum -= c;
                k -= c;
            }
            else{
                resultSum -= k;
                k = 0;
            }
           }
           out.println(resultSum);
        }

        
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

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

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

    static class Task {
        public void solve(InputReader in, PrintWriter out) {
           long a  = in.nextLong();
           long b = in.nextLong();
           long c = in.nextLong();
           long k = in.nextLong();
            long resultSum = 0;
           if(k > 0){
               if(k > a){
                   resultSum += a;
                   k -= a;
               }
               else{
                   resultSum += k;
                   k = 0;
               }
           }
           if(k > 0){
               k -= (k  > b)? b : k;
           }

           if(k > 0){
            if(k > c){
                resultSum -= c;
                k -= c;
            }
            else{
                resultSum -= k;
                k = 0;
            }
           }
           out.println(resultSum);
        }

        
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

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

    }
} Main public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(in, out);
        out.close();
    } main {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(in, out);
        out.close();
    } InputStream inputStream = System.in; inputStream System.in System System.in OutputStream outputStream = System.out; outputStream System.out System System.out InputReader in = new InputReader(inputStream); in new InputReader(inputStream) PrintWriter out = new PrintWriter(outputStream); out new PrintWriter(outputStream) Task solver = new Task(); solver new Task() solver.solve(in, out) solver.solve solver in out out.close() out.close out String[] args args static class Task {
        public void solve(InputReader in, PrintWriter out) {
           long a  = in.nextLong();
           long b = in.nextLong();
           long c = in.nextLong();
           long k = in.nextLong();
            long resultSum = 0;
           if(k > 0){
               if(k > a){
                   resultSum += a;
                   k -= a;
               }
               else{
                   resultSum += k;
                   k = 0;
               }
           }
           if(k > 0){
               k -= (k  > b)? b : k;
           }

           if(k > 0){
            if(k > c){
                resultSum -= c;
                k -= c;
            }
            else{
                resultSum -= k;
                k = 0;
            }
           }
           out.println(resultSum);
        }

        
    } Task public void solve(InputReader in, PrintWriter out) {
           long a  = in.nextLong();
           long b = in.nextLong();
           long c = in.nextLong();
           long k = in.nextLong();
            long resultSum = 0;
           if(k > 0){
               if(k > a){
                   resultSum += a;
                   k -= a;
               }
               else{
                   resultSum += k;
                   k = 0;
               }
           }
           if(k > 0){
               k -= (k  > b)? b : k;
           }

           if(k > 0){
            if(k > c){
                resultSum -= c;
                k -= c;
            }
            else{
                resultSum -= k;
                k = 0;
            }
           }
           out.println(resultSum);
        } solve {
           long a  = in.nextLong();
           long b = in.nextLong();
           long c = in.nextLong();
           long k = in.nextLong();
            long resultSum = 0;
           if(k > 0){
               if(k > a){
                   resultSum += a;
                   k -= a;
               }
               else{
                   resultSum += k;
                   k = 0;
               }
           }
           if(k > 0){
               k -= (k  > b)? b : k;
           }

           if(k > 0){
            if(k > c){
                resultSum -= c;
                k -= c;
            }
            else{
                resultSum -= k;
                k = 0;
            }
           }
           out.println(resultSum);
        } 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 resultSum = 0; resultSum 0 if(k > 0){
               if(k > a){
                   resultSum += a;
                   k -= a;
               }
               else{
                   resultSum += k;
                   k = 0;
               }
           } k > 0 k 0 {
               if(k > a){
                   resultSum += a;
                   k -= a;
               }
               else{
                   resultSum += k;
                   k = 0;
               }
           } if(k > a){
                   resultSum += a;
                   k -= a;
               }
               else{
                   resultSum += k;
                   k = 0;
               } k > a k a {
                   resultSum += a;
                   k -= a;
               } resultSum += a resultSum a k -= a k a {
                   resultSum += k;
                   k = 0;
               } resultSum += k resultSum k k = 0 k 0 if(k > 0){
               k -= (k  > b)? b : k;
           } k > 0 k 0 {
               k -= (k  > b)? b : k;
           } k -= (k  > b)? b : k k (k  > b)? b : k (k  > b) k b b k if(k > 0){
            if(k > c){
                resultSum -= c;
                k -= c;
            }
            else{
                resultSum -= k;
                k = 0;
            }
           } k > 0 k 0 {
            if(k > c){
                resultSum -= c;
                k -= c;
            }
            else{
                resultSum -= k;
                k = 0;
            }
           } if(k > c){
                resultSum -= c;
                k -= c;
            }
            else{
                resultSum -= k;
                k = 0;
            } k > c k c {
                resultSum -= c;
                k -= c;
            } resultSum -= c resultSum c k -= c k c {
                resultSum -= k;
                k = 0;
            } resultSum -= k resultSum k k = 0 k 0 out.println(resultSum) out.println out resultSum InputReader in in PrintWriter out out static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

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

    } InputReader public BufferedReader reader; reader public StringTokenizer tokenizer; tokenizer public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        } InputReader {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        } reader = new BufferedReader(new InputStreamReader(stream), 32768) reader new BufferedReader(new InputStreamReader(stream), 32768) tokenizer = null tokenizer null InputStream stream stream public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        } next {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        } while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } tokenizer == null || !tokenizer.hasMoreTokens() tokenizer == null tokenizer null !tokenizer.hasMoreTokens() tokenizer.hasMoreTokens() tokenizer.hasMoreTokens tokenizer {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } IOException e {
                    throw new RuntimeException(e);
                } throw new RuntimeException(e); new RuntimeException(e) {
                    tokenizer = new StringTokenizer(reader.readLine());
                } tokenizer = new StringTokenizer(reader.readLine()) tokenizer new StringTokenizer(reader.readLine()) return tokenizer.nextToken(); tokenizer.nextToken() tokenizer.nextToken tokenizer public int nextInt() {
            return Integer.parseInt(next());
        } nextInt {
            return Integer.parseInt(next());
        } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public long nextLong(){
          return Long.parseLong(next());
        } nextLong {
          return Long.parseLong(next());
        } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(in, out);
        out.close();
    }

    static class Task {
        public void solve(InputReader in, PrintWriter out) {
           long a  = in.nextLong();
           long b = in.nextLong();
           long c = in.nextLong();
           long k = in.nextLong();
            long resultSum = 0;
           if(k > 0){
               if(k > a){
                   resultSum += a;
                   k -= a;
               }
               else{
                   resultSum += k;
                   k = 0;
               }
           }
           if(k > 0){
               k -= (k  > b)? b : k;
           }

           if(k > 0){
            if(k > c){
                resultSum -= c;
                k -= c;
            }
            else{
                resultSum -= k;
                k = 0;
            }
           }
           out.println(resultSum);
        }

        
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

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

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

    static class Task {
        public void solve(InputReader in, PrintWriter out) {
           long a  = in.nextLong();
           long b = in.nextLong();
           long c = in.nextLong();
           long k = in.nextLong();
            long resultSum = 0;
           if(k > 0){
               if(k > a){
                   resultSum += a;
                   k -= a;
               }
               else{
                   resultSum += k;
                   k = 0;
               }
           }
           if(k > 0){
               k -= (k  > b)? b : k;
           }

           if(k > 0){
            if(k > c){
                resultSum -= c;
                k -= c;
            }
            else{
                resultSum -= k;
                k = 0;
            }
           }
           out.println(resultSum);
        }

        
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

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

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

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

    static class Task {
        public void solve(InputReader in, PrintWriter out) {
           long a  = in.nextLong();
           long b = in.nextLong();
           long c = in.nextLong();
           long k = in.nextLong();
            long resultSum = 0;
           if(k > 0){
               if(k > a){
                   resultSum += a;
                   k -= a;
               }
               else{
                   resultSum += k;
                   k = 0;
               }
           }
           if(k > 0){
               k -= (k  > b)? b : k;
           }

           if(k > 0){
            if(k > c){
                resultSum -= c;
                k -= c;
            }
            else{
                resultSum -= k;
                k = 0;
            }
           }
           out.println(resultSum);
        }

        
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

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

    }
}

