import 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();
        if((A + B) >= K ) {
			if (A > K)
		System.out.println(K);
			else
		System.out.println(A);
		}
		else if((A + B) < K ) {
		System.out.println(A + (K - A -B) * - 1);
		}
    }
} import java.util.*; util.* java 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();
        if((A + B) >= K ) {
			if (A > K)
		System.out.println(K);
			else
		System.out.println(A);
		}
		else if((A + B) < K ) {
		System.out.println(A + (K - A -B) * - 1);
		}
    }
} 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();
        if((A + B) >= K ) {
			if (A > K)
		System.out.println(K);
			else
		System.out.println(A);
		}
		else if((A + B) < K ) {
		System.out.println(A + (K - A -B) * - 1);
		}
    } main {
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        int K = sc.nextInt();
        if((A + B) >= K ) {
			if (A > K)
		System.out.println(K);
			else
		System.out.println(A);
		}
		else if((A + B) < K ) {
		System.out.println(A + (K - A -B) * - 1);
		}
    } 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 if((A + B) >= K ) {
			if (A > K)
		System.out.println(K);
			else
		System.out.println(A);
		}
		else if((A + B) < K ) {
		System.out.println(A + (K - A -B) * - 1);
		} (A + B) >= K (A + B) A B K {
			if (A > K)
		System.out.println(K);
			else
		System.out.println(A);
		} if (A > K)
		System.out.println(K);
			else
		System.out.println(A); A > K A K System.out.println(K) System.out.println System.out System System.out K System.out.println(A) System.out.println System.out System System.out A if((A + B) < K ) {
		System.out.println(A + (K - A -B) * - 1);
		} (A + B) < K (A + B) A B K {
		System.out.println(A + (K - A -B) * - 1);
		} System.out.println(A + (K - A -B) * - 1) System.out.println System.out System System.out A + (K - A -B) * - 1 A (K - A -B) * - 1 (K - A -B) K - A -B K A B - 1 1 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();
        if((A + B) >= K ) {
			if (A > K)
		System.out.println(K);
			else
		System.out.println(A);
		}
		else if((A + B) < K ) {
		System.out.println(A + (K - A -B) * - 1);
		}
    }
} 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();
        if((A + B) >= K ) {
			if (A > K)
		System.out.println(K);
			else
		System.out.println(A);
		}
		else if((A + B) < K ) {
		System.out.println(A + (K - A -B) * - 1);
		}
    }
} Main
import 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();
        if((A + B) >= K ) {
			if (A > K)
		System.out.println(K);
			else
		System.out.println(A);
		}
		else if((A + B) < K ) {
		System.out.println(A + (K - A -B) * - 1);
		}
    }
}