import java.util.Scanner;
class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s = new Scanner(System.in);
		int a = s.nextInt();
		int b = s.nextInt();
		int c = s.nextInt();
		int k = s.nextInt();
		int sum = 0;
		if (k > a) {
			k = k - a;
			sum += a;
			if (k > b) {
				k = k - b;
				if (k > 0) {
					sum -= k;
				}
			}
		} else {
			sum = k;
		}
		System.out.println(sum);
	}

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

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s = new Scanner(System.in);
		int a = s.nextInt();
		int b = s.nextInt();
		int c = s.nextInt();
		int k = s.nextInt();
		int sum = 0;
		if (k > a) {
			k = k - a;
			sum += a;
			if (k > b) {
				k = k - b;
				if (k > 0) {
					sum -= k;
				}
			}
		} else {
			sum = k;
		}
		System.out.println(sum);
	}

} Main public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s = new Scanner(System.in);
		int a = s.nextInt();
		int b = s.nextInt();
		int c = s.nextInt();
		int k = s.nextInt();
		int sum = 0;
		if (k > a) {
			k = k - a;
			sum += a;
			if (k > b) {
				k = k - b;
				if (k > 0) {
					sum -= k;
				}
			}
		} else {
			sum = k;
		}
		System.out.println(sum);
	} main {
		// TODO Auto-generated method stub
		Scanner s = new Scanner(System.in);
		int a = s.nextInt();
		int b = s.nextInt();
		int c = s.nextInt();
		int k = s.nextInt();
		int sum = 0;
		if (k > a) {
			k = k - a;
			sum += a;
			if (k > b) {
				k = k - b;
				if (k > 0) {
					sum -= k;
				}
			}
		} else {
			sum = k;
		}
		System.out.println(sum);
	} Scanner s = new Scanner(System.in); s new Scanner(System.in) int a = s.nextInt(); a s.nextInt() s.nextInt s int b = s.nextInt(); b s.nextInt() s.nextInt s int c = s.nextInt(); c s.nextInt() s.nextInt s int k = s.nextInt(); k s.nextInt() s.nextInt s int sum = 0; sum 0 if (k > a) {
			k = k - a;
			sum += a;
			if (k > b) {
				k = k - b;
				if (k > 0) {
					sum -= k;
				}
			}
		} else {
			sum = k;
		} k > a k a {
			k = k - a;
			sum += a;
			if (k > b) {
				k = k - b;
				if (k > 0) {
					sum -= k;
				}
			}
		} k = k - a k k - a k a sum += a sum a if (k > b) {
				k = k - b;
				if (k > 0) {
					sum -= k;
				}
			} k > b k b {
				k = k - b;
				if (k > 0) {
					sum -= k;
				}
			} k = k - b k k - b k b if (k > 0) {
					sum -= k;
				} k > 0 k 0 {
					sum -= k;
				} sum -= k sum k {
			sum = k;
		} sum = k sum k System.out.println(sum) System.out.println System.out System System.out sum String[] args args
import java.util.Scanner;
class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s = new Scanner(System.in);
		int a = s.nextInt();
		int b = s.nextInt();
		int c = s.nextInt();
		int k = s.nextInt();
		int sum = 0;
		if (k > a) {
			k = k - a;
			sum += a;
			if (k > b) {
				k = k - b;
				if (k > 0) {
					sum -= k;
				}
			}
		} else {
			sum = k;
		}
		System.out.println(sum);
	}

}
