import java.util.*;

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 sumb = 0;
        int sumc = 0;

        /*
        if(K >= A) {
            sumb = K - A;
            if(sumb > 0) {
                sumc = K - A - B;
            }
            System.out.println(A - sumc);
        } else System.out.println(K);
        */

        //別解
        int xa = Math.min(A,K);
        K -= xa;
        int xb = Math.min(B,K);
        K -= xb;
        int xc = K;
        int ans = xa -xc;
        System.out.println(ans);

    }
} import java.util.*; util.* java 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 sumb = 0;
        int sumc = 0;

        /*
        if(K >= A) {
            sumb = K - A;
            if(sumb > 0) {
                sumc = K - A - B;
            }
            System.out.println(A - sumc);
        } else System.out.println(K);
        */

        //別解
        int xa = Math.min(A,K);
        K -= xa;
        int xb = Math.min(B,K);
        K -= xb;
        int xc = K;
        int ans = xa -xc;
        System.out.println(ans);

    }
} 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 sumb = 0;
        int sumc = 0;

        /*
        if(K >= A) {
            sumb = K - A;
            if(sumb > 0) {
                sumc = K - A - B;
            }
            System.out.println(A - sumc);
        } else System.out.println(K);
        */

        //別解
        int xa = Math.min(A,K);
        K -= xa;
        int xb = Math.min(B,K);
        K -= xb;
        int xc = K;
        int ans = xa -xc;
        System.out.println(ans);

    } main {
		Scanner sc = new Scanner(System.in);
        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();
        int sumb = 0;
        int sumc = 0;

        /*
        if(K >= A) {
            sumb = K - A;
            if(sumb > 0) {
                sumc = K - A - B;
            }
            System.out.println(A - sumc);
        } else System.out.println(K);
        */

        //別解
        int xa = Math.min(A,K);
        K -= xa;
        int xb = Math.min(B,K);
        K -= xb;
        int xc = K;
        int ans = xa -xc;
        System.out.println(ans);

    } 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 sumb = 0; sumb 0 int sumc = 0; sumc 0 int xa = Math.min(A,K); xa Math.min(A,K) Math.min Math A K K -= xa K xa int xb = Math.min(B,K); xb Math.min(B,K) Math.min Math B K K -= xb K xb int xc = K; xc K int ans = xa -xc; ans xa -xc xa xc System.out.println(ans) System.out.println System.out System System.out ans String[] args args
import java.util.*;

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 sumb = 0;
        int sumc = 0;

        /*
        if(K >= A) {
            sumb = K - A;
            if(sumb > 0) {
                sumc = K - A - B;
            }
            System.out.println(A - sumc);
        } else System.out.println(K);
        */

        //別解
        int xa = Math.min(A,K);
        K -= xa;
        int xb = Math.min(B,K);
        K -= xb;
        int xc = K;
        int ans = xa -xc;
        System.out.println(ans);

    }
}