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

		int inputa = sc.nextInt();
		int inputb = sc.nextInt();
		int inputc = sc.nextInt();
		int inputk = sc.nextInt();

		int result = 0;
		if(inputk <= inputa) {
			result = inputk;
		}
		else if(inputk <= inputa + inputb) {
			result = inputa;
		}
		else if(inputk < inputa + inputb + inputc) {
			result = inputa - (inputk - inputa - inputb);
		}else {
			result = inputa - inputc;
		}
		System.out.println(result);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int inputa = sc.nextInt();
		int inputb = sc.nextInt();
		int inputc = sc.nextInt();
		int inputk = sc.nextInt();

		int result = 0;
		if(inputk <= inputa) {
			result = inputk;
		}
		else if(inputk <= inputa + inputb) {
			result = inputa;
		}
		else if(inputk < inputa + inputb + inputc) {
			result = inputa - (inputk - inputa - inputb);
		}else {
			result = inputa - inputc;
		}
		System.out.println(result);
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int inputa = sc.nextInt();
		int inputb = sc.nextInt();
		int inputc = sc.nextInt();
		int inputk = sc.nextInt();

		int result = 0;
		if(inputk <= inputa) {
			result = inputk;
		}
		else if(inputk <= inputa + inputb) {
			result = inputa;
		}
		else if(inputk < inputa + inputb + inputc) {
			result = inputa - (inputk - inputa - inputb);
		}else {
			result = inputa - inputc;
		}
		System.out.println(result);
	} main {
		Scanner sc = new Scanner(System.in);

		int inputa = sc.nextInt();
		int inputb = sc.nextInt();
		int inputc = sc.nextInt();
		int inputk = sc.nextInt();

		int result = 0;
		if(inputk <= inputa) {
			result = inputk;
		}
		else if(inputk <= inputa + inputb) {
			result = inputa;
		}
		else if(inputk < inputa + inputb + inputc) {
			result = inputa - (inputk - inputa - inputb);
		}else {
			result = inputa - inputc;
		}
		System.out.println(result);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int inputa = sc.nextInt(); inputa sc.nextInt() sc.nextInt sc int inputb = sc.nextInt(); inputb sc.nextInt() sc.nextInt sc int inputc = sc.nextInt(); inputc sc.nextInt() sc.nextInt sc int inputk = sc.nextInt(); inputk sc.nextInt() sc.nextInt sc int result = 0; result 0 if(inputk <= inputa) {
			result = inputk;
		}
		else if(inputk <= inputa + inputb) {
			result = inputa;
		}
		else if(inputk < inputa + inputb + inputc) {
			result = inputa - (inputk - inputa - inputb);
		}else {
			result = inputa - inputc;
		} inputk <= inputa inputk inputa {
			result = inputk;
		} result = inputk result inputk if(inputk <= inputa + inputb) {
			result = inputa;
		}
		else if(inputk < inputa + inputb + inputc) {
			result = inputa - (inputk - inputa - inputb);
		}else {
			result = inputa - inputc;
		} inputk <= inputa + inputb inputk inputa + inputb inputa inputb {
			result = inputa;
		} result = inputa result inputa if(inputk < inputa + inputb + inputc) {
			result = inputa - (inputk - inputa - inputb);
		}else {
			result = inputa - inputc;
		} inputk < inputa + inputb + inputc inputk inputa + inputb + inputc inputa + inputb + inputc inputa inputb inputc {
			result = inputa - (inputk - inputa - inputb);
		} result = inputa - (inputk - inputa - inputb) result inputa - (inputk - inputa - inputb) inputa (inputk - inputa - inputb) inputk - inputa - inputb inputk inputa inputb {
			result = inputa - inputc;
		} result = inputa - inputc result inputa - inputc inputa inputc System.out.println(result) System.out.println System.out System System.out result String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int inputa = sc.nextInt();
		int inputb = sc.nextInt();
		int inputc = sc.nextInt();
		int inputk = sc.nextInt();

		int result = 0;
		if(inputk <= inputa) {
			result = inputk;
		}
		else if(inputk <= inputa + inputb) {
			result = inputa;
		}
		else if(inputk < inputa + inputb + inputc) {
			result = inputa - (inputk - inputa - inputb);
		}else {
			result = inputa - inputc;
		}
		System.out.println(result);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int inputa = sc.nextInt();
		int inputb = sc.nextInt();
		int inputc = sc.nextInt();
		int inputk = sc.nextInt();

		int result = 0;
		if(inputk <= inputa) {
			result = inputk;
		}
		else if(inputk <= inputa + inputb) {
			result = inputa;
		}
		else if(inputk < inputa + inputb + inputc) {
			result = inputa - (inputk - inputa - inputb);
		}else {
			result = inputa - inputc;
		}
		System.out.println(result);
	}
} Main
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int inputa = sc.nextInt();
		int inputb = sc.nextInt();
		int inputc = sc.nextInt();
		int inputk = sc.nextInt();

		int result = 0;
		if(inputk <= inputa) {
			result = inputk;
		}
		else if(inputk <= inputa + inputb) {
			result = inputa;
		}
		else if(inputk < inputa + inputb + inputc) {
			result = inputa - (inputk - inputa - inputb);
		}else {
			result = inputa - inputc;
		}
		System.out.println(result);
	}
}