import java.util.*;

public class Main {

    public static void main(String args[]) {


        Scanner scan = new Scanner(System.in);

        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int k = scan.nextInt();

        int d = a + b;

        if (a >= k) {
            System.out.println(String.valueOf(k));
            return;
        }


            if (d >= k) {
                System.out.println(String.valueOf(a));
            } else {
                int e = k - d;
                System.out.println(String.valueOf(a - e));
            }
        }
    

} import java.util.*; util.* java public class Main {

    public static void main(String args[]) {


        Scanner scan = new Scanner(System.in);

        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int k = scan.nextInt();

        int d = a + b;

        if (a >= k) {
            System.out.println(String.valueOf(k));
            return;
        }


            if (d >= k) {
                System.out.println(String.valueOf(a));
            } else {
                int e = k - d;
                System.out.println(String.valueOf(a - e));
            }
        }
    

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


        Scanner scan = new Scanner(System.in);

        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int k = scan.nextInt();

        int d = a + b;

        if (a >= k) {
            System.out.println(String.valueOf(k));
            return;
        }


            if (d >= k) {
                System.out.println(String.valueOf(a));
            } else {
                int e = k - d;
                System.out.println(String.valueOf(a - e));
            }
        } main {


        Scanner scan = new Scanner(System.in);

        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int k = scan.nextInt();

        int d = a + b;

        if (a >= k) {
            System.out.println(String.valueOf(k));
            return;
        }


            if (d >= k) {
                System.out.println(String.valueOf(a));
            } else {
                int e = k - d;
                System.out.println(String.valueOf(a - e));
            }
        } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int a = scan.nextInt(); a scan.nextInt() scan.nextInt scan int b = scan.nextInt(); b scan.nextInt() scan.nextInt scan int c = scan.nextInt(); c scan.nextInt() scan.nextInt scan int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int d = a + b; d a + b a b if (a >= k) {
            System.out.println(String.valueOf(k));
            return;
        } a >= k a k {
            System.out.println(String.valueOf(k));
            return;
        } System.out.println(String.valueOf(k)) System.out.println System.out System System.out String.valueOf(k) String.valueOf String k return; if (d >= k) {
                System.out.println(String.valueOf(a));
            } else {
                int e = k - d;
                System.out.println(String.valueOf(a - e));
            } d >= k d k {
                System.out.println(String.valueOf(a));
            } System.out.println(String.valueOf(a)) System.out.println System.out System System.out String.valueOf(a) String.valueOf String a {
                int e = k - d;
                System.out.println(String.valueOf(a - e));
            } int e = k - d; e k - d k d System.out.println(String.valueOf(a - e)) System.out.println System.out System System.out String.valueOf(a - e) String.valueOf String a - e a e String args[] args public class Main {

    public static void main(String args[]) {


        Scanner scan = new Scanner(System.in);

        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int k = scan.nextInt();

        int d = a + b;

        if (a >= k) {
            System.out.println(String.valueOf(k));
            return;
        }


            if (d >= k) {
                System.out.println(String.valueOf(a));
            } else {
                int e = k - d;
                System.out.println(String.valueOf(a - e));
            }
        }
    

} public class Main {

    public static void main(String args[]) {


        Scanner scan = new Scanner(System.in);

        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int k = scan.nextInt();

        int d = a + b;

        if (a >= k) {
            System.out.println(String.valueOf(k));
            return;
        }


            if (d >= k) {
                System.out.println(String.valueOf(a));
            } else {
                int e = k - d;
                System.out.println(String.valueOf(a - e));
            }
        }
    

} Main
import java.util.*;

public class Main {

    public static void main(String args[]) {


        Scanner scan = new Scanner(System.in);

        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int k = scan.nextInt();

        int d = a + b;

        if (a >= k) {
            System.out.println(String.valueOf(k));
            return;
        }


            if (d >= k) {
                System.out.println(String.valueOf(a));
            } else {
                int e = k - d;
                System.out.println(String.valueOf(a - e));
            }
        }
    

}