import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		
		Scanner in = new Scanner(System.in);
		
		long A = in.nextLong();
		long B = in.nextLong();
		long C = in.nextLong();
		long K = in.nextLong();
		
		
		//[1,2,3] K=4
		
		if (K<A) {
			System.out.println(K);
		} else {
			if (K==A) {
				System.out.println(A);
			} else if (A+B >= K) {
				System.out.println(A);
			} else {
				System.out.println(A - (K-A-B));
			}
		}
		
		
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String args[]) {
		
		Scanner in = new Scanner(System.in);
		
		long A = in.nextLong();
		long B = in.nextLong();
		long C = in.nextLong();
		long K = in.nextLong();
		
		
		//[1,2,3] K=4
		
		if (K<A) {
			System.out.println(K);
		} else {
			if (K==A) {
				System.out.println(A);
			} else if (A+B >= K) {
				System.out.println(A);
			} else {
				System.out.println(A - (K-A-B));
			}
		}
		
		
	}
} Main public static void main(String args[]) {
		
		Scanner in = new Scanner(System.in);
		
		long A = in.nextLong();
		long B = in.nextLong();
		long C = in.nextLong();
		long K = in.nextLong();
		
		
		//[1,2,3] K=4
		
		if (K<A) {
			System.out.println(K);
		} else {
			if (K==A) {
				System.out.println(A);
			} else if (A+B >= K) {
				System.out.println(A);
			} else {
				System.out.println(A - (K-A-B));
			}
		}
		
		
	} main {
		
		Scanner in = new Scanner(System.in);
		
		long A = in.nextLong();
		long B = in.nextLong();
		long C = in.nextLong();
		long K = in.nextLong();
		
		
		//[1,2,3] K=4
		
		if (K<A) {
			System.out.println(K);
		} else {
			if (K==A) {
				System.out.println(A);
			} else if (A+B >= K) {
				System.out.println(A);
			} else {
				System.out.println(A - (K-A-B));
			}
		}
		
		
	} Scanner in = new Scanner(System.in); in new Scanner(System.in) long A = in.nextLong(); A in.nextLong() in.nextLong in long B = in.nextLong(); B in.nextLong() in.nextLong in long C = in.nextLong(); C in.nextLong() in.nextLong in long K = in.nextLong(); K in.nextLong() in.nextLong in if (K<A) {
			System.out.println(K);
		} else {
			if (K==A) {
				System.out.println(A);
			} else if (A+B >= K) {
				System.out.println(A);
			} else {
				System.out.println(A - (K-A-B));
			}
		} K<A K A {
			System.out.println(K);
		} System.out.println(K) System.out.println System.out System System.out K {
			if (K==A) {
				System.out.println(A);
			} else if (A+B >= K) {
				System.out.println(A);
			} else {
				System.out.println(A - (K-A-B));
			}
		} if (K==A) {
				System.out.println(A);
			} else if (A+B >= K) {
				System.out.println(A);
			} else {
				System.out.println(A - (K-A-B));
			} K==A K A {
				System.out.println(A);
			} System.out.println(A) System.out.println System.out System System.out A if (A+B >= K) {
				System.out.println(A);
			} else {
				System.out.println(A - (K-A-B));
			} A+B >= K A+B A B K {
				System.out.println(A);
			} System.out.println(A) System.out.println System.out System System.out A {
				System.out.println(A - (K-A-B));
			} System.out.println(A - (K-A-B)) System.out.println System.out System System.out A - (K-A-B) A (K-A-B) K-A-B K A B String args[] args public class Main {
	public static void main(String args[]) {
		
		Scanner in = new Scanner(System.in);
		
		long A = in.nextLong();
		long B = in.nextLong();
		long C = in.nextLong();
		long K = in.nextLong();
		
		
		//[1,2,3] K=4
		
		if (K<A) {
			System.out.println(K);
		} else {
			if (K==A) {
				System.out.println(A);
			} else if (A+B >= K) {
				System.out.println(A);
			} else {
				System.out.println(A - (K-A-B));
			}
		}
		
		
	}
} public class Main {
	public static void main(String args[]) {
		
		Scanner in = new Scanner(System.in);
		
		long A = in.nextLong();
		long B = in.nextLong();
		long C = in.nextLong();
		long K = in.nextLong();
		
		
		//[1,2,3] K=4
		
		if (K<A) {
			System.out.println(K);
		} else {
			if (K==A) {
				System.out.println(A);
			} else if (A+B >= K) {
				System.out.println(A);
			} else {
				System.out.println(A - (K-A-B));
			}
		}
		
		
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		
		Scanner in = new Scanner(System.in);
		
		long A = in.nextLong();
		long B = in.nextLong();
		long C = in.nextLong();
		long K = in.nextLong();
		
		
		//[1,2,3] K=4
		
		if (K<A) {
			System.out.println(K);
		} else {
			if (K==A) {
				System.out.println(A);
			} else if (A+B >= K) {
				System.out.println(A);
			} else {
				System.out.println(A - (K-A-B));
			}
		}
		
		
	}
}
