import java.util.Scanner;

public class Main {

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

		long A = sc.nextInt();
		long B = sc.nextInt();
		long C = sc.nextInt();
		long K = sc.nextInt();
		long ans = 0;

		if (A < K){
			ans = A;
			K -= A;
		} else {
			ans = K;
			System.out.println(ans);
			return;
		}

		if (B < K){
			K -= B;
		} else {
			System.out.println(ans);
			return;
		}

		ans += -1 * K;
		System.out.println(ans);
	}

} import java.util.Scanner; Scanner java.util public class Main {

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

		long A = sc.nextInt();
		long B = sc.nextInt();
		long C = sc.nextInt();
		long K = sc.nextInt();
		long ans = 0;

		if (A < K){
			ans = A;
			K -= A;
		} else {
			ans = K;
			System.out.println(ans);
			return;
		}

		if (B < K){
			K -= B;
		} else {
			System.out.println(ans);
			return;
		}

		ans += -1 * K;
		System.out.println(ans);
	}

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

		long A = sc.nextInt();
		long B = sc.nextInt();
		long C = sc.nextInt();
		long K = sc.nextInt();
		long ans = 0;

		if (A < K){
			ans = A;
			K -= A;
		} else {
			ans = K;
			System.out.println(ans);
			return;
		}

		if (B < K){
			K -= B;
		} else {
			System.out.println(ans);
			return;
		}

		ans += -1 * K;
		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);

		long A = sc.nextInt();
		long B = sc.nextInt();
		long C = sc.nextInt();
		long K = sc.nextInt();
		long ans = 0;

		if (A < K){
			ans = A;
			K -= A;
		} else {
			ans = K;
			System.out.println(ans);
			return;
		}

		if (B < K){
			K -= B;
		} else {
			System.out.println(ans);
			return;
		}

		ans += -1 * K;
		System.out.println(ans);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long A = sc.nextInt(); A sc.nextInt() sc.nextInt sc long B = sc.nextInt(); B sc.nextInt() sc.nextInt sc long C = sc.nextInt(); C sc.nextInt() sc.nextInt sc long K = sc.nextInt(); K sc.nextInt() sc.nextInt sc long ans = 0; ans 0 if (A < K){
			ans = A;
			K -= A;
		} else {
			ans = K;
			System.out.println(ans);
			return;
		} A < K A K {
			ans = A;
			K -= A;
		} ans = A ans A K -= A K A {
			ans = K;
			System.out.println(ans);
			return;
		} ans = K ans K System.out.println(ans) System.out.println System.out System System.out ans return; if (B < K){
			K -= B;
		} else {
			System.out.println(ans);
			return;
		} B < K B K {
			K -= B;
		} K -= B K B {
			System.out.println(ans);
			return;
		} System.out.println(ans) System.out.println System.out System System.out ans return; ans += -1 * K ans -1 * K -1 1 K System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {

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

		long A = sc.nextInt();
		long B = sc.nextInt();
		long C = sc.nextInt();
		long K = sc.nextInt();
		long ans = 0;

		if (A < K){
			ans = A;
			K -= A;
		} else {
			ans = K;
			System.out.println(ans);
			return;
		}

		if (B < K){
			K -= B;
		} else {
			System.out.println(ans);
			return;
		}

		ans += -1 * K;
		System.out.println(ans);
	}

} public class Main {

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

		long A = sc.nextInt();
		long B = sc.nextInt();
		long C = sc.nextInt();
		long K = sc.nextInt();
		long ans = 0;

		if (A < K){
			ans = A;
			K -= A;
		} else {
			ans = K;
			System.out.println(ans);
			return;
		}

		if (B < K){
			K -= B;
		} else {
			System.out.println(ans);
			return;
		}

		ans += -1 * K;
		System.out.println(ans);
	}

} Main
import java.util.Scanner;

public class Main {

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

		long A = sc.nextInt();
		long B = sc.nextInt();
		long C = sc.nextInt();
		long K = sc.nextInt();
		long ans = 0;

		if (A < K){
			ans = A;
			K -= A;
		} else {
			ans = K;
			System.out.println(ans);
			return;
		}

		if (B < K){
			K -= B;
		} else {
			System.out.println(ans);
			return;
		}

		ans += -1 * K;
		System.out.println(ans);
	}

}
