import java.util.Scanner;

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

        Scanner sc = new Scanner(System.in);

        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();
        int point = 0;
        int diff;

        if (A >= K) {
            point = K;
        } else if ((A + B) >= K) {
            point = A;
        } else {
            diff = K - (A + B);
            point = A - diff;
        }

        System.out.print(point);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();
        int point = 0;
        int diff;

        if (A >= K) {
            point = K;
        } else if ((A + B) >= K) {
            point = A;
        } else {
            diff = K - (A + B);
            point = A - diff;
        }

        System.out.print(point);
    }
} Main public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();
        int point = 0;
        int diff;

        if (A >= K) {
            point = K;
        } else if ((A + B) >= K) {
            point = A;
        } else {
            diff = K - (A + B);
            point = A - diff;
        }

        System.out.print(point);
    } main {

        Scanner sc = new Scanner(System.in);

        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();
        int point = 0;
        int diff;

        if (A >= K) {
            point = K;
        } else if ((A + B) >= K) {
            point = A;
        } else {
            diff = K - (A + B);
            point = A - diff;
        }

        System.out.print(point);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int A = sc.nextInt(); A sc.nextInt() sc.nextInt sc int B = sc.nextInt(); B sc.nextInt() sc.nextInt sc int C = sc.nextInt(); C sc.nextInt() sc.nextInt sc int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int point = 0; point 0 int diff; diff if (A >= K) {
            point = K;
        } else if ((A + B) >= K) {
            point = A;
        } else {
            diff = K - (A + B);
            point = A - diff;
        } A >= K A K {
            point = K;
        } point = K point K if ((A + B) >= K) {
            point = A;
        } else {
            diff = K - (A + B);
            point = A - diff;
        } (A + B) >= K (A + B) A B K {
            point = A;
        } point = A point A {
            diff = K - (A + B);
            point = A - diff;
        } diff = K - (A + B) diff K - (A + B) K (A + B) A B point = A - diff point A - diff A diff System.out.print(point) System.out.print System.out System System.out point String[] args args public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();
        int point = 0;
        int diff;

        if (A >= K) {
            point = K;
        } else if ((A + B) >= K) {
            point = A;
        } else {
            diff = K - (A + B);
            point = A - diff;
        }

        System.out.print(point);
    }
} public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();
        int point = 0;
        int diff;

        if (A >= K) {
            point = K;
        } else if ((A + B) >= K) {
            point = A;
        } else {
            diff = K - (A + B);
            point = A - diff;
        }

        System.out.print(point);
    }
} Main
import java.util.Scanner;

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

        Scanner sc = new Scanner(System.in);

        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();
        int point = 0;
        int diff;

        if (A >= K) {
            point = K;
        } else if ((A + B) >= K) {
            point = A;
        } else {
            diff = K - (A + B);
            point = A - diff;
        }

        System.out.print(point);
    }
}