import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner scn = new Scanner(System.in);
		long a = scn.nextLong();
		long b = scn.nextLong();
		long c = scn.nextLong();
		long k = scn.nextLong();
		System.out.println(Ret(a, b, c, k));
	}

	public static long Ret(long a, long b, long c, long k) {
		long ans = 0;
		while (a != 0) {
			ans = ans + 1;
			k = k - 1;
			a = a - 1;
			if (k <= 0) {
				return ans;
			}
		}
		k = k - b;
		while (k > 0) {
			ans = ans - 1;
			k = k - 1;
		}
		return ans;
	}

} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner scn = new Scanner(System.in);
		long a = scn.nextLong();
		long b = scn.nextLong();
		long c = scn.nextLong();
		long k = scn.nextLong();
		System.out.println(Ret(a, b, c, k));
	}

	public static long Ret(long a, long b, long c, long k) {
		long ans = 0;
		while (a != 0) {
			ans = ans + 1;
			k = k - 1;
			a = a - 1;
			if (k <= 0) {
				return ans;
			}
		}
		k = k - b;
		while (k > 0) {
			ans = ans - 1;
			k = k - 1;
		}
		return ans;
	}

} Main public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner scn = new Scanner(System.in);
		long a = scn.nextLong();
		long b = scn.nextLong();
		long c = scn.nextLong();
		long k = scn.nextLong();
		System.out.println(Ret(a, b, c, k));
	} main {
		@SuppressWarnings("resource")
		Scanner scn = new Scanner(System.in);
		long a = scn.nextLong();
		long b = scn.nextLong();
		long c = scn.nextLong();
		long k = scn.nextLong();
		System.out.println(Ret(a, b, c, k));
	} @SuppressWarnings("resource")
		Scanner scn = new Scanner(System.in); scn new Scanner(System.in) long a = scn.nextLong(); a scn.nextLong() scn.nextLong scn long b = scn.nextLong(); b scn.nextLong() scn.nextLong scn long c = scn.nextLong(); c scn.nextLong() scn.nextLong scn long k = scn.nextLong(); k scn.nextLong() scn.nextLong scn System.out.println(Ret(a, b, c, k)) System.out.println System.out System System.out Ret(a, b, c, k) Ret a b c k String[] args args public static long Ret(long a, long b, long c, long k) {
		long ans = 0;
		while (a != 0) {
			ans = ans + 1;
			k = k - 1;
			a = a - 1;
			if (k <= 0) {
				return ans;
			}
		}
		k = k - b;
		while (k > 0) {
			ans = ans - 1;
			k = k - 1;
		}
		return ans;
	} Ret {
		long ans = 0;
		while (a != 0) {
			ans = ans + 1;
			k = k - 1;
			a = a - 1;
			if (k <= 0) {
				return ans;
			}
		}
		k = k - b;
		while (k > 0) {
			ans = ans - 1;
			k = k - 1;
		}
		return ans;
	} long ans = 0; ans 0 while (a != 0) {
			ans = ans + 1;
			k = k - 1;
			a = a - 1;
			if (k <= 0) {
				return ans;
			}
		} a != 0 a 0 {
			ans = ans + 1;
			k = k - 1;
			a = a - 1;
			if (k <= 0) {
				return ans;
			}
		} ans = ans + 1 ans ans + 1 ans 1 k = k - 1 k k - 1 k 1 a = a - 1 a a - 1 a 1 if (k <= 0) {
				return ans;
			} k <= 0 k 0 {
				return ans;
			} return ans; ans k = k - b k k - b k b while (k > 0) {
			ans = ans - 1;
			k = k - 1;
		} k > 0 k 0 {
			ans = ans - 1;
			k = k - 1;
		} ans = ans - 1 ans ans - 1 ans 1 k = k - 1 k k - 1 k 1 return ans; ans long a a long b b long c c long k k public class Main {
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner scn = new Scanner(System.in);
		long a = scn.nextLong();
		long b = scn.nextLong();
		long c = scn.nextLong();
		long k = scn.nextLong();
		System.out.println(Ret(a, b, c, k));
	}

	public static long Ret(long a, long b, long c, long k) {
		long ans = 0;
		while (a != 0) {
			ans = ans + 1;
			k = k - 1;
			a = a - 1;
			if (k <= 0) {
				return ans;
			}
		}
		k = k - b;
		while (k > 0) {
			ans = ans - 1;
			k = k - 1;
		}
		return ans;
	}

} public class Main {
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner scn = new Scanner(System.in);
		long a = scn.nextLong();
		long b = scn.nextLong();
		long c = scn.nextLong();
		long k = scn.nextLong();
		System.out.println(Ret(a, b, c, k));
	}

	public static long Ret(long a, long b, long c, long k) {
		long ans = 0;
		while (a != 0) {
			ans = ans + 1;
			k = k - 1;
			a = a - 1;
			if (k <= 0) {
				return ans;
			}
		}
		k = k - b;
		while (k > 0) {
			ans = ans - 1;
			k = k - 1;
		}
		return ans;
	}

} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner scn = new Scanner(System.in);
		long a = scn.nextLong();
		long b = scn.nextLong();
		long c = scn.nextLong();
		long k = scn.nextLong();
		System.out.println(Ret(a, b, c, k));
	}

	public static long Ret(long a, long b, long c, long k) {
		long ans = 0;
		while (a != 0) {
			ans = ans + 1;
			k = k - 1;
			a = a - 1;
			if (k <= 0) {
				return ans;
			}
		}
		k = k - b;
		while (k > 0) {
			ans = ans - 1;
			k = k - 1;
		}
		return ans;
	}

}