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

public class Main {
    public static void main(String[] args) throws Exception {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            
            String[] tmp = br.readLine().split(" ");
            int a = Integer.parseInt(tmp[0]);
            int b = Integer.parseInt(tmp[1]);
            int c = Integer.parseInt(tmp[2]);
            int k = Integer.parseInt(tmp[3]);
            
            br.close();
            
            PrintWriter pw = new PrintWriter(System.out);
            
            if (k <= a) {
                System.out.println(k);
            } else if (k <= a + b) {
                System.out.println(a);
            } else {
                System.out.println(a - (k - a - b));
            }
            
            pw.flush();
            
        }
    }
} import java.io.*; io.* java import java.util.*; util.* java public class Main {
    public static void main(String[] args) throws Exception {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            
            String[] tmp = br.readLine().split(" ");
            int a = Integer.parseInt(tmp[0]);
            int b = Integer.parseInt(tmp[1]);
            int c = Integer.parseInt(tmp[2]);
            int k = Integer.parseInt(tmp[3]);
            
            br.close();
            
            PrintWriter pw = new PrintWriter(System.out);
            
            if (k <= a) {
                System.out.println(k);
            } else if (k <= a + b) {
                System.out.println(a);
            } else {
                System.out.println(a - (k - a - b));
            }
            
            pw.flush();
            
        }
    }
} Main public static void main(String[] args) throws Exception {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            
            String[] tmp = br.readLine().split(" ");
            int a = Integer.parseInt(tmp[0]);
            int b = Integer.parseInt(tmp[1]);
            int c = Integer.parseInt(tmp[2]);
            int k = Integer.parseInt(tmp[3]);
            
            br.close();
            
            PrintWriter pw = new PrintWriter(System.out);
            
            if (k <= a) {
                System.out.println(k);
            } else if (k <= a + b) {
                System.out.println(a);
            } else {
                System.out.println(a - (k - a - b));
            }
            
            pw.flush();
            
        }
    } main {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            
            String[] tmp = br.readLine().split(" ");
            int a = Integer.parseInt(tmp[0]);
            int b = Integer.parseInt(tmp[1]);
            int c = Integer.parseInt(tmp[2]);
            int k = Integer.parseInt(tmp[3]);
            
            br.close();
            
            PrintWriter pw = new PrintWriter(System.out);
            
            if (k <= a) {
                System.out.println(k);
            } else if (k <= a + b) {
                System.out.println(a);
            } else {
                System.out.println(a - (k - a - b));
            }
            
            pw.flush();
            
        }
    } try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            
            String[] tmp = br.readLine().split(" ");
            int a = Integer.parseInt(tmp[0]);
            int b = Integer.parseInt(tmp[1]);
            int c = Integer.parseInt(tmp[2]);
            int k = Integer.parseInt(tmp[3]);
            
            br.close();
            
            PrintWriter pw = new PrintWriter(System.out);
            
            if (k <= a) {
                System.out.println(k);
            } else if (k <= a + b) {
                System.out.println(a);
            } else {
                System.out.println(a - (k - a - b));
            }
            
            pw.flush();
            
        } {
            
            String[] tmp = br.readLine().split(" ");
            int a = Integer.parseInt(tmp[0]);
            int b = Integer.parseInt(tmp[1]);
            int c = Integer.parseInt(tmp[2]);
            int k = Integer.parseInt(tmp[3]);
            
            br.close();
            
            PrintWriter pw = new PrintWriter(System.out);
            
            if (k <= a) {
                System.out.println(k);
            } else if (k <= a + b) {
                System.out.println(a);
            } else {
                System.out.println(a - (k - a - b));
            }
            
            pw.flush();
            
        } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) new BufferedReader(new InputStreamReader(System.in)) String[] tmp = br.readLine().split(" "); tmp br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int a = Integer.parseInt(tmp[0]); a Integer.parseInt(tmp[0]) Integer.parseInt Integer tmp[0] tmp 0 int b = Integer.parseInt(tmp[1]); b Integer.parseInt(tmp[1]) Integer.parseInt Integer tmp[1] tmp 1 int c = Integer.parseInt(tmp[2]); c Integer.parseInt(tmp[2]) Integer.parseInt Integer tmp[2] tmp 2 int k = Integer.parseInt(tmp[3]); k Integer.parseInt(tmp[3]) Integer.parseInt Integer tmp[3] tmp 3 br.close() br.close br PrintWriter pw = new PrintWriter(System.out); pw new PrintWriter(System.out) if (k <= a) {
                System.out.println(k);
            } else if (k <= a + b) {
                System.out.println(a);
            } else {
                System.out.println(a - (k - a - b));
            } k <= a k a {
                System.out.println(k);
            } System.out.println(k) System.out.println System.out System System.out k if (k <= a + b) {
                System.out.println(a);
            } else {
                System.out.println(a - (k - a - b));
            } k <= a + b k a + b a b {
                System.out.println(a);
            } System.out.println(a) System.out.println System.out System System.out a {
                System.out.println(a - (k - a - b));
            } System.out.println(a - (k - a - b)) System.out.println System.out System System.out a - (k - a - b) a (k - a - b) k - a - b k a b pw.flush() pw.flush pw String[] args args public class Main {
    public static void main(String[] args) throws Exception {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            
            String[] tmp = br.readLine().split(" ");
            int a = Integer.parseInt(tmp[0]);
            int b = Integer.parseInt(tmp[1]);
            int c = Integer.parseInt(tmp[2]);
            int k = Integer.parseInt(tmp[3]);
            
            br.close();
            
            PrintWriter pw = new PrintWriter(System.out);
            
            if (k <= a) {
                System.out.println(k);
            } else if (k <= a + b) {
                System.out.println(a);
            } else {
                System.out.println(a - (k - a - b));
            }
            
            pw.flush();
            
        }
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            
            String[] tmp = br.readLine().split(" ");
            int a = Integer.parseInt(tmp[0]);
            int b = Integer.parseInt(tmp[1]);
            int c = Integer.parseInt(tmp[2]);
            int k = Integer.parseInt(tmp[3]);
            
            br.close();
            
            PrintWriter pw = new PrintWriter(System.out);
            
            if (k <= a) {
                System.out.println(k);
            } else if (k <= a + b) {
                System.out.println(a);
            } else {
                System.out.println(a - (k - a - b));
            }
            
            pw.flush();
            
        }
    }
} Main
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            
            String[] tmp = br.readLine().split(" ");
            int a = Integer.parseInt(tmp[0]);
            int b = Integer.parseInt(tmp[1]);
            int c = Integer.parseInt(tmp[2]);
            int k = Integer.parseInt(tmp[3]);
            
            br.close();
            
            PrintWriter pw = new PrintWriter(System.out);
            
            if (k <= a) {
                System.out.println(k);
            } else if (k <= a + b) {
                System.out.println(a);
            } else {
                System.out.println(a - (k - a - b));
            }
            
            pw.flush();
            
        }
    }
}