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 sum = 0;

        for (int i = 0; i < A && i < K; i++) {
            sum++;
        }
        for (int i = (A + B); i < K; i++) {
            sum--;
        }
        System.out.println(sum);
        sc.close();
    }
} 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 sum = 0;

        for (int i = 0; i < A && i < K; i++) {
            sum++;
        }
        for (int i = (A + B); i < K; i++) {
            sum--;
        }
        System.out.println(sum);
        sc.close();
    }
} 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 sum = 0;

        for (int i = 0; i < A && i < K; i++) {
            sum++;
        }
        for (int i = (A + B); i < K; i++) {
            sum--;
        }
        System.out.println(sum);
        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);

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

        int sum = 0;

        for (int i = 0; i < A && i < K; i++) {
            sum++;
        }
        for (int i = (A + B); i < K; i++) {
            sum--;
        }
        System.out.println(sum);
        sc.close();
    } 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 sum = 0; sum 0 for (int i = 0; i < A && i < K; i++) {
            sum++;
        } int i = 0; int i = 0; i 0 i < A && i < K i < A i A i < K i K i++ i++ i {
            sum++;
        } {
            sum++;
        } sum++ sum for (int i = (A + B); i < K; i++) {
            sum--;
        } int i = (A + B); int i = (A + B); i (A + B) A B i < K i K i++ i++ i {
            sum--;
        } {
            sum--;
        } sum-- sum System.out.println(sum) System.out.println System.out System System.out sum sc.close() sc.close sc 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 sum = 0;

        for (int i = 0; i < A && i < K; i++) {
            sum++;
        }
        for (int i = (A + B); i < K; i++) {
            sum--;
        }
        System.out.println(sum);
        sc.close();
    }
} 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 sum = 0;

        for (int i = 0; i < A && i < K; i++) {
            sum++;
        }
        for (int i = (A + B); i < K; i++) {
            sum--;
        }
        System.out.println(sum);
        sc.close();
    }
} 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 sum = 0;

        for (int i = 0; i < A && i < K; i++) {
            sum++;
        }
        for (int i = (A + B); i < K; i++) {
            sum--;
        }
        System.out.println(sum);
        sc.close();
    }
}