import java.util.Scanner;
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 ans = 0;

		for(int i = 0; i < k; i++) {
			if(a >= 1) {
				a--;
				ans++;
			} else if(b >= 1) {
				b--;
			} else {
				c--;
				ans--;
			}
		}

		System.out.println(ans);
		sc.close();
	}

} import java.util.Scanner; Scanner 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 ans = 0;

		for(int i = 0; i < k; i++) {
			if(a >= 1) {
				a--;
				ans++;
			} else if(b >= 1) {
				b--;
			} else {
				c--;
				ans--;
			}
		}

		System.out.println(ans);
		sc.close();
	}

} 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 ans = 0;

		for(int i = 0; i < k; i++) {
			if(a >= 1) {
				a--;
				ans++;
			} else if(b >= 1) {
				b--;
			} else {
				c--;
				ans--;
			}
		}

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

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		int ans = 0;

		for(int i = 0; i < k; i++) {
			if(a >= 1) {
				a--;
				ans++;
			} else if(b >= 1) {
				b--;
			} else {
				c--;
				ans--;
			}
		}

		System.out.println(ans);
		sc.close();
	} 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 ans = 0; ans 0 for(int i = 0; i < k; i++) {
			if(a >= 1) {
				a--;
				ans++;
			} else if(b >= 1) {
				b--;
			} else {
				c--;
				ans--;
			}
		} int i = 0; int i = 0; i 0 i < k i k i++ i++ i {
			if(a >= 1) {
				a--;
				ans++;
			} else if(b >= 1) {
				b--;
			} else {
				c--;
				ans--;
			}
		} {
			if(a >= 1) {
				a--;
				ans++;
			} else if(b >= 1) {
				b--;
			} else {
				c--;
				ans--;
			}
		} if(a >= 1) {
				a--;
				ans++;
			} else if(b >= 1) {
				b--;
			} else {
				c--;
				ans--;
			} a >= 1 a 1 {
				a--;
				ans++;
			} a-- a ans++ ans if(b >= 1) {
				b--;
			} else {
				c--;
				ans--;
			} b >= 1 b 1 {
				b--;
			} b-- b {
				c--;
				ans--;
			} c-- c ans-- ans System.out.println(ans) System.out.println System.out System System.out ans sc.close() sc.close sc String[] args args
import java.util.Scanner;
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 ans = 0;

		for(int i = 0; i < k; i++) {
			if(a >= 1) {
				a--;
				ans++;
			} else if(b >= 1) {
				b--;
			} else {
				c--;
				ans--;
			}
		}

		System.out.println(ans);
		sc.close();
	}

}