import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int A = Integer.parseInt(scan.next());
        int B = Integer.parseInt(scan.next());
        int C = Integer.parseInt(scan.next());
        int K = Integer.parseInt(scan.next());

        if (A >= K) {
            System.out.println(K);
        } else if ((A + B) >= K) {
            System.out.println(A);
        } else {
            System.out.println(A - (K - (A + B)));
        }
    }
} import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int A = Integer.parseInt(scan.next());
        int B = Integer.parseInt(scan.next());
        int C = Integer.parseInt(scan.next());
        int K = Integer.parseInt(scan.next());

        if (A >= K) {
            System.out.println(K);
        } else if ((A + B) >= K) {
            System.out.println(A);
        } else {
            System.out.println(A - (K - (A + B)));
        }
    }
} Main public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int A = Integer.parseInt(scan.next());
        int B = Integer.parseInt(scan.next());
        int C = Integer.parseInt(scan.next());
        int K = Integer.parseInt(scan.next());

        if (A >= K) {
            System.out.println(K);
        } else if ((A + B) >= K) {
            System.out.println(A);
        } else {
            System.out.println(A - (K - (A + B)));
        }
    } main {
        Scanner scan = new Scanner(System.in);
        int A = Integer.parseInt(scan.next());
        int B = Integer.parseInt(scan.next());
        int C = Integer.parseInt(scan.next());
        int K = Integer.parseInt(scan.next());

        if (A >= K) {
            System.out.println(K);
        } else if ((A + B) >= K) {
            System.out.println(A);
        } else {
            System.out.println(A - (K - (A + B)));
        }
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int A = Integer.parseInt(scan.next()); A Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan int B = Integer.parseInt(scan.next()); B Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan int C = Integer.parseInt(scan.next()); C Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan int K = Integer.parseInt(scan.next()); K Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan if (A >= K) {
            System.out.println(K);
        } else if ((A + B) >= K) {
            System.out.println(A);
        } else {
            System.out.println(A - (K - (A + B)));
        } 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 {
            System.out.println(A - (K - (A + B)));
        } (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 {
            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) A B String[] args args public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int A = Integer.parseInt(scan.next());
        int B = Integer.parseInt(scan.next());
        int C = Integer.parseInt(scan.next());
        int K = Integer.parseInt(scan.next());

        if (A >= K) {
            System.out.println(K);
        } else if ((A + B) >= K) {
            System.out.println(A);
        } else {
            System.out.println(A - (K - (A + B)));
        }
    }
} public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int A = Integer.parseInt(scan.next());
        int B = Integer.parseInt(scan.next());
        int C = Integer.parseInt(scan.next());
        int K = Integer.parseInt(scan.next());

        if (A >= K) {
            System.out.println(K);
        } else if ((A + B) >= K) {
            System.out.println(A);
        } else {
            System.out.println(A - (K - (A + B)));
        }
    }
} Main
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int A = Integer.parseInt(scan.next());
        int B = Integer.parseInt(scan.next());
        int C = Integer.parseInt(scan.next());
        int K = Integer.parseInt(scan.next());

        if (A >= K) {
            System.out.println(K);
        } else if ((A + B) >= K) {
            System.out.println(A);
        } else {
            System.out.println(A - (K - (A + B)));
        }
    }
}