import java.util.Scanner;

public 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 res = 0;
		if (k - a <= 0) {
			System.out.println(k);
		} else {
			res += a;
			k -= a;
			if (k - b <= 0) {
				System.out.println(res);
			} else {
				k -= b;
				System.out.println(res + -1 * k);
			}
		}
	}
} import java.util.Scanner; Scanner java.util public 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 res = 0;
		if (k - a <= 0) {
			System.out.println(k);
		} else {
			res += a;
			k -= a;
			if (k - b <= 0) {
				System.out.println(res);
			} else {
				k -= b;
				System.out.println(res + -1 * k);
			}
		}
	}
} 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 res = 0;
		if (k - a <= 0) {
			System.out.println(k);
		} else {
			res += a;
			k -= a;
			if (k - b <= 0) {
				System.out.println(res);
			} else {
				k -= b;
				System.out.println(res + -1 * k);
			}
		}
	} main {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		int res = 0;
		if (k - a <= 0) {
			System.out.println(k);
		} else {
			res += a;
			k -= a;
			if (k - b <= 0) {
				System.out.println(res);
			} else {
				k -= b;
				System.out.println(res + -1 * k);
			}
		}
	} 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 res = 0; res 0 if (k - a <= 0) {
			System.out.println(k);
		} else {
			res += a;
			k -= a;
			if (k - b <= 0) {
				System.out.println(res);
			} else {
				k -= b;
				System.out.println(res + -1 * k);
			}
		} k - a <= 0 k - a k a 0 {
			System.out.println(k);
		} System.out.println(k) System.out.println System.out System System.out k {
			res += a;
			k -= a;
			if (k - b <= 0) {
				System.out.println(res);
			} else {
				k -= b;
				System.out.println(res + -1 * k);
			}
		} res += a res a k -= a k a if (k - b <= 0) {
				System.out.println(res);
			} else {
				k -= b;
				System.out.println(res + -1 * k);
			} k - b <= 0 k - b k b 0 {
				System.out.println(res);
			} System.out.println(res) System.out.println System.out System System.out res {
				k -= b;
				System.out.println(res + -1 * k);
			} k -= b k b System.out.println(res + -1 * k) System.out.println System.out System System.out res + -1 * k res -1 * k -1 1 k String[] args args public 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 res = 0;
		if (k - a <= 0) {
			System.out.println(k);
		} else {
			res += a;
			k -= a;
			if (k - b <= 0) {
				System.out.println(res);
			} else {
				k -= b;
				System.out.println(res + -1 * k);
			}
		}
	}
} public 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 res = 0;
		if (k - a <= 0) {
			System.out.println(k);
		} else {
			res += a;
			k -= a;
			if (k - b <= 0) {
				System.out.println(res);
			} else {
				k -= b;
				System.out.println(res + -1 * k);
			}
		}
	}
} Main
import java.util.Scanner;

public 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 res = 0;
		if (k - a <= 0) {
			System.out.println(k);
		} else {
			res += a;
			k -= a;
			if (k - b <= 0) {
				System.out.println(res);
			} else {
				k -= b;
				System.out.println(res + -1 * k);
			}
		}
	}
}
