

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();

		int ans = 0;

		if (A > K) {
			ans = 1 * K;
			System.out.println(ans);
		} else {
			K = K - A;
			ans = 1 * A;
			if (K == 0) {
				System.out.println(ans);
			} else {
				K = K - B;
				ans = ans + (B * 0);
				if (K <= 0) {
					System.out.println(ans);
				} else {
					ans = ans + (K * -1);
					System.out.println(ans);
				}

			}

		}
	}

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

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();

		int ans = 0;

		if (A > K) {
			ans = 1 * K;
			System.out.println(ans);
		} else {
			K = K - A;
			ans = 1 * A;
			if (K == 0) {
				System.out.println(ans);
			} else {
				K = K - B;
				ans = ans + (B * 0);
				if (K <= 0) {
					System.out.println(ans);
				} else {
					ans = ans + (K * -1);
					System.out.println(ans);
				}

			}

		}
	}

} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();

		int ans = 0;

		if (A > K) {
			ans = 1 * K;
			System.out.println(ans);
		} else {
			K = K - A;
			ans = 1 * A;
			if (K == 0) {
				System.out.println(ans);
			} else {
				K = K - B;
				ans = ans + (B * 0);
				if (K <= 0) {
					System.out.println(ans);
				} else {
					ans = ans + (K * -1);
					System.out.println(ans);
				}

			}

		}
	} main {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();

		int ans = 0;

		if (A > K) {
			ans = 1 * K;
			System.out.println(ans);
		} else {
			K = K - A;
			ans = 1 * A;
			if (K == 0) {
				System.out.println(ans);
			} else {
				K = K - B;
				ans = ans + (B * 0);
				if (K <= 0) {
					System.out.println(ans);
				} else {
					ans = ans + (K * -1);
					System.out.println(ans);
				}

			}

		}
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int A = scan.nextInt(); A scan.nextInt() scan.nextInt scan int B = scan.nextInt(); B scan.nextInt() scan.nextInt scan int C = scan.nextInt(); C scan.nextInt() scan.nextInt scan int K = scan.nextInt(); K scan.nextInt() scan.nextInt scan int ans = 0; ans 0 if (A > K) {
			ans = 1 * K;
			System.out.println(ans);
		} else {
			K = K - A;
			ans = 1 * A;
			if (K == 0) {
				System.out.println(ans);
			} else {
				K = K - B;
				ans = ans + (B * 0);
				if (K <= 0) {
					System.out.println(ans);
				} else {
					ans = ans + (K * -1);
					System.out.println(ans);
				}

			}

		} A > K A K {
			ans = 1 * K;
			System.out.println(ans);
		} ans = 1 * K ans 1 * K 1 K System.out.println(ans) System.out.println System.out System System.out ans {
			K = K - A;
			ans = 1 * A;
			if (K == 0) {
				System.out.println(ans);
			} else {
				K = K - B;
				ans = ans + (B * 0);
				if (K <= 0) {
					System.out.println(ans);
				} else {
					ans = ans + (K * -1);
					System.out.println(ans);
				}

			}

		} K = K - A K K - A K A ans = 1 * A ans 1 * A 1 A if (K == 0) {
				System.out.println(ans);
			} else {
				K = K - B;
				ans = ans + (B * 0);
				if (K <= 0) {
					System.out.println(ans);
				} else {
					ans = ans + (K * -1);
					System.out.println(ans);
				}

			} K == 0 K 0 {
				System.out.println(ans);
			} System.out.println(ans) System.out.println System.out System System.out ans {
				K = K - B;
				ans = ans + (B * 0);
				if (K <= 0) {
					System.out.println(ans);
				} else {
					ans = ans + (K * -1);
					System.out.println(ans);
				}

			} K = K - B K K - B K B ans = ans + (B * 0) ans ans + (B * 0) ans (B * 0) B 0 if (K <= 0) {
					System.out.println(ans);
				} else {
					ans = ans + (K * -1);
					System.out.println(ans);
				} K <= 0 K 0 {
					System.out.println(ans);
				} System.out.println(ans) System.out.println System.out System System.out ans {
					ans = ans + (K * -1);
					System.out.println(ans);
				} ans = ans + (K * -1) ans ans + (K * -1) ans (K * -1) K -1 1 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) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();

		int ans = 0;

		if (A > K) {
			ans = 1 * K;
			System.out.println(ans);
		} else {
			K = K - A;
			ans = 1 * A;
			if (K == 0) {
				System.out.println(ans);
			} else {
				K = K - B;
				ans = ans + (B * 0);
				if (K <= 0) {
					System.out.println(ans);
				} else {
					ans = ans + (K * -1);
					System.out.println(ans);
				}

			}

		}
	}

} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();

		int ans = 0;

		if (A > K) {
			ans = 1 * K;
			System.out.println(ans);
		} else {
			K = K - A;
			ans = 1 * A;
			if (K == 0) {
				System.out.println(ans);
			} else {
				K = K - B;
				ans = ans + (B * 0);
				if (K <= 0) {
					System.out.println(ans);
				} else {
					ans = ans + (K * -1);
					System.out.println(ans);
				}

			}

		}
	}

} Main


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();

		int ans = 0;

		if (A > K) {
			ans = 1 * K;
			System.out.println(ans);
		} else {
			K = K - A;
			ans = 1 * A;
			if (K == 0) {
				System.out.println(ans);
			} else {
				K = K - B;
				ans = ans + (B * 0);
				if (K <= 0) {
					System.out.println(ans);
				} else {
					ans = ans + (K * -1);
					System.out.println(ans);
				}

			}

		}
	}

}
