import java.util.*;

public class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);

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

        int count = K;
        int rst = 0;

        // A 
        rst += Math.min(count, A);
        count = Math.max(0, count - A);

        // B 
        count = Math.max(0, count - B);

        // C 
        rst -= Math.min(count, C);
        count = Math.max(0, count - C);

        System.out.println(rst);
    }
} import java.util.*; util.* java public class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);

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

        int count = K;
        int rst = 0;

        // A 
        rst += Math.min(count, A);
        count = Math.max(0, count - A);

        // B 
        count = Math.max(0, count - B);

        // C 
        rst -= Math.min(count, C);
        count = Math.max(0, count - C);

        System.out.println(rst);
    }
} Main public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);

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

        int count = K;
        int rst = 0;

        // A 
        rst += Math.min(count, A);
        count = Math.max(0, count - A);

        // B 
        count = Math.max(0, count - B);

        // C 
        rst -= Math.min(count, C);
        count = Math.max(0, count - C);

        System.out.println(rst);
    } main {
        Scanner sc = new Scanner(System.in);

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

        int count = K;
        int rst = 0;

        // A 
        rst += Math.min(count, A);
        count = Math.max(0, count - A);

        // B 
        count = Math.max(0, count - B);

        // C 
        rst -= Math.min(count, C);
        count = Math.max(0, count - C);

        System.out.println(rst);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) Integer A = sc.nextInt(); A sc.nextInt() sc.nextInt sc Integer B = sc.nextInt(); B sc.nextInt() sc.nextInt sc Integer C = sc.nextInt(); C sc.nextInt() sc.nextInt sc Integer K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int count = K; count K int rst = 0; rst 0 rst += Math.min(count, A) rst Math.min(count, A) Math.min Math count A count = Math.max(0, count - A) count Math.max(0, count - A) Math.max Math 0 count - A count A count = Math.max(0, count - B) count Math.max(0, count - B) Math.max Math 0 count - B count B rst -= Math.min(count, C) rst Math.min(count, C) Math.min Math count C count = Math.max(0, count - C) count Math.max(0, count - C) Math.max Math 0 count - C count C System.out.println(rst) System.out.println System.out System System.out rst String[] args args public class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);

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

        int count = K;
        int rst = 0;

        // A 
        rst += Math.min(count, A);
        count = Math.max(0, count - A);

        // B 
        count = Math.max(0, count - B);

        // C 
        rst -= Math.min(count, C);
        count = Math.max(0, count - C);

        System.out.println(rst);
    }
} public class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);

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

        int count = K;
        int rst = 0;

        // A 
        rst += Math.min(count, A);
        count = Math.max(0, count - A);

        // B 
        count = Math.max(0, count - B);

        // C 
        rst -= Math.min(count, C);
        count = Math.max(0, count - C);

        System.out.println(rst);
    }
} Main
import java.util.*;

public class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);

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

        int count = K;
        int rst = 0;

        // A 
        rst += Math.min(count, A);
        count = Math.max(0, count - A);

        // B 
        count = Math.max(0, count - B);

        // C 
        rst -= Math.min(count, C);
        count = Math.max(0, count - C);

        System.out.println(rst);
    }
}