import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) throws Exception {

        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(is);

        ArrayList<String> mylist = new ArrayList<>();

        // 標準入力からの値を変数strinputに代入
        String strinput = reader.readLine();

        while (strinput != null) {
            mylist.add(strinput);
            strinput = reader.readLine();
        }

        String arr01[] = mylist.get(0).split(" ");

        int a = Integer.valueOf(arr01[0]);
        int b = Integer.valueOf(arr01[1]);
        int c = Integer.valueOf(arr01[2]);
        int k = Integer.valueOf(arr01[3]);

        if (a >= k) {
            System.out.println(k);
        } else if (a + b >= k) {
            System.out.println(a);
        } else {
            int nokori = k - (a + b);
            System.out.println(a - Math.min(c, nokori));

        }

    }

} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.ArrayList; ArrayList java.util public class Main {

    public static void main(String[] args) throws Exception {

        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(is);

        ArrayList<String> mylist = new ArrayList<>();

        // 標準入力からの値を変数strinputに代入
        String strinput = reader.readLine();

        while (strinput != null) {
            mylist.add(strinput);
            strinput = reader.readLine();
        }

        String arr01[] = mylist.get(0).split(" ");

        int a = Integer.valueOf(arr01[0]);
        int b = Integer.valueOf(arr01[1]);
        int c = Integer.valueOf(arr01[2]);
        int k = Integer.valueOf(arr01[3]);

        if (a >= k) {
            System.out.println(k);
        } else if (a + b >= k) {
            System.out.println(a);
        } else {
            int nokori = k - (a + b);
            System.out.println(a - Math.min(c, nokori));

        }

    }

} Main public static void main(String[] args) throws Exception {

        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(is);

        ArrayList<String> mylist = new ArrayList<>();

        // 標準入力からの値を変数strinputに代入
        String strinput = reader.readLine();

        while (strinput != null) {
            mylist.add(strinput);
            strinput = reader.readLine();
        }

        String arr01[] = mylist.get(0).split(" ");

        int a = Integer.valueOf(arr01[0]);
        int b = Integer.valueOf(arr01[1]);
        int c = Integer.valueOf(arr01[2]);
        int k = Integer.valueOf(arr01[3]);

        if (a >= k) {
            System.out.println(k);
        } else if (a + b >= k) {
            System.out.println(a);
        } else {
            int nokori = k - (a + b);
            System.out.println(a - Math.min(c, nokori));

        }

    } main {

        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(is);

        ArrayList<String> mylist = new ArrayList<>();

        // 標準入力からの値を変数strinputに代入
        String strinput = reader.readLine();

        while (strinput != null) {
            mylist.add(strinput);
            strinput = reader.readLine();
        }

        String arr01[] = mylist.get(0).split(" ");

        int a = Integer.valueOf(arr01[0]);
        int b = Integer.valueOf(arr01[1]);
        int c = Integer.valueOf(arr01[2]);
        int k = Integer.valueOf(arr01[3]);

        if (a >= k) {
            System.out.println(k);
        } else if (a + b >= k) {
            System.out.println(a);
        } else {
            int nokori = k - (a + b);
            System.out.println(a - Math.min(c, nokori));

        }

    } InputStreamReader is = new InputStreamReader(System.in); is new InputStreamReader(System.in) BufferedReader reader = new BufferedReader(is); reader new BufferedReader(is) ArrayList<String> mylist = new ArrayList<>(); mylist new ArrayList<>() String strinput = reader.readLine(); strinput reader.readLine() reader.readLine reader while (strinput != null) {
            mylist.add(strinput);
            strinput = reader.readLine();
        } strinput != null strinput null {
            mylist.add(strinput);
            strinput = reader.readLine();
        } mylist.add(strinput) mylist.add mylist strinput strinput = reader.readLine() strinput reader.readLine() reader.readLine reader String arr01[] = mylist.get(0).split(" "); arr01 mylist.get(0).split(" ") mylist.get(0).split mylist.get(0) mylist.get mylist 0 " " int a = Integer.valueOf(arr01[0]); a Integer.valueOf(arr01[0]) Integer.valueOf Integer arr01[0] arr01 0 int b = Integer.valueOf(arr01[1]); b Integer.valueOf(arr01[1]) Integer.valueOf Integer arr01[1] arr01 1 int c = Integer.valueOf(arr01[2]); c Integer.valueOf(arr01[2]) Integer.valueOf Integer arr01[2] arr01 2 int k = Integer.valueOf(arr01[3]); k Integer.valueOf(arr01[3]) Integer.valueOf Integer arr01[3] arr01 3 if (a >= k) {
            System.out.println(k);
        } else if (a + b >= k) {
            System.out.println(a);
        } else {
            int nokori = k - (a + b);
            System.out.println(a - Math.min(c, nokori));

        } a >= k a k {
            System.out.println(k);
        } System.out.println(k) System.out.println System.out System System.out k if (a + b >= k) {
            System.out.println(a);
        } else {
            int nokori = k - (a + b);
            System.out.println(a - Math.min(c, nokori));

        } a + b >= k a + b a b k {
            System.out.println(a);
        } System.out.println(a) System.out.println System.out System System.out a {
            int nokori = k - (a + b);
            System.out.println(a - Math.min(c, nokori));

        } int nokori = k - (a + b); nokori k - (a + b) k (a + b) a b System.out.println(a - Math.min(c, nokori)) System.out.println System.out System System.out a - Math.min(c, nokori) a Math.min(c, nokori) Math.min Math c nokori String[] args args public class Main {

    public static void main(String[] args) throws Exception {

        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(is);

        ArrayList<String> mylist = new ArrayList<>();

        // 標準入力からの値を変数strinputに代入
        String strinput = reader.readLine();

        while (strinput != null) {
            mylist.add(strinput);
            strinput = reader.readLine();
        }

        String arr01[] = mylist.get(0).split(" ");

        int a = Integer.valueOf(arr01[0]);
        int b = Integer.valueOf(arr01[1]);
        int c = Integer.valueOf(arr01[2]);
        int k = Integer.valueOf(arr01[3]);

        if (a >= k) {
            System.out.println(k);
        } else if (a + b >= k) {
            System.out.println(a);
        } else {
            int nokori = k - (a + b);
            System.out.println(a - Math.min(c, nokori));

        }

    }

} public class Main {

    public static void main(String[] args) throws Exception {

        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(is);

        ArrayList<String> mylist = new ArrayList<>();

        // 標準入力からの値を変数strinputに代入
        String strinput = reader.readLine();

        while (strinput != null) {
            mylist.add(strinput);
            strinput = reader.readLine();
        }

        String arr01[] = mylist.get(0).split(" ");

        int a = Integer.valueOf(arr01[0]);
        int b = Integer.valueOf(arr01[1]);
        int c = Integer.valueOf(arr01[2]);
        int k = Integer.valueOf(arr01[3]);

        if (a >= k) {
            System.out.println(k);
        } else if (a + b >= k) {
            System.out.println(a);
        } else {
            int nokori = k - (a + b);
            System.out.println(a - Math.min(c, nokori));

        }

    }

} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) throws Exception {

        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(is);

        ArrayList<String> mylist = new ArrayList<>();

        // 標準入力からの値を変数strinputに代入
        String strinput = reader.readLine();

        while (strinput != null) {
            mylist.add(strinput);
            strinput = reader.readLine();
        }

        String arr01[] = mylist.get(0).split(" ");

        int a = Integer.valueOf(arr01[0]);
        int b = Integer.valueOf(arr01[1]);
        int c = Integer.valueOf(arr01[2]);
        int k = Integer.valueOf(arr01[3]);

        if (a >= k) {
            System.out.println(k);
        } else if (a + b >= k) {
            System.out.println(a);
        } else {
            int nokori = k - (a + b);
            System.out.println(a - Math.min(c, nokori));

        }

    }

}
