import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt(); // 1
		int B = sc.nextInt(); // 0
		int C = sc.nextInt(); // -1
		long K = sc.nextLong(); // 取るカードの枚数
		sc.close();
		long answer = 0L;
		if(K <= A) {
			answer = K;
		} else if(K <= (A + B)) {
			answer = A;
		} else {
			answer = A - (K - A - B); 
		}
		System.out.println(answer);
	}
} 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(); // 1
		int B = sc.nextInt(); // 0
		int C = sc.nextInt(); // -1
		long K = sc.nextLong(); // 取るカードの枚数
		sc.close();
		long answer = 0L;
		if(K <= A) {
			answer = K;
		} else if(K <= (A + B)) {
			answer = A;
		} else {
			answer = A - (K - A - B); 
		}
		System.out.println(answer);
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt(); // 1
		int B = sc.nextInt(); // 0
		int C = sc.nextInt(); // -1
		long K = sc.nextLong(); // 取るカードの枚数
		sc.close();
		long answer = 0L;
		if(K <= A) {
			answer = K;
		} else if(K <= (A + B)) {
			answer = A;
		} else {
			answer = A - (K - A - B); 
		}
		System.out.println(answer);
	} main {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt(); // 1
		int B = sc.nextInt(); // 0
		int C = sc.nextInt(); // -1
		long K = sc.nextLong(); // 取るカードの枚数
		sc.close();
		long answer = 0L;
		if(K <= A) {
			answer = K;
		} else if(K <= (A + B)) {
			answer = A;
		} else {
			answer = A - (K - A - B); 
		}
		System.out.println(answer);
	} 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 long K = sc.nextLong(); K sc.nextLong() sc.nextLong sc sc.close() sc.close sc long answer = 0L; answer 0L if(K <= A) {
			answer = K;
		} else if(K <= (A + B)) {
			answer = A;
		} else {
			answer = A - (K - A - B); 
		} K <= A K A {
			answer = K;
		} answer = K answer K if(K <= (A + B)) {
			answer = A;
		} else {
			answer = A - (K - A - B); 
		} K <= (A + B) K (A + B) A B {
			answer = A;
		} answer = A answer A {
			answer = A - (K - A - B); 
		} answer = A - (K - A - B) answer A - (K - A - B) A (K - A - B) K - A - B K A B System.out.println(answer) System.out.println System.out System System.out answer String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt(); // 1
		int B = sc.nextInt(); // 0
		int C = sc.nextInt(); // -1
		long K = sc.nextLong(); // 取るカードの枚数
		sc.close();
		long answer = 0L;
		if(K <= A) {
			answer = K;
		} else if(K <= (A + B)) {
			answer = A;
		} else {
			answer = A - (K - A - B); 
		}
		System.out.println(answer);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt(); // 1
		int B = sc.nextInt(); // 0
		int C = sc.nextInt(); // -1
		long K = sc.nextLong(); // 取るカードの枚数
		sc.close();
		long answer = 0L;
		if(K <= A) {
			answer = K;
		} else if(K <= (A + B)) {
			answer = A;
		} else {
			answer = A - (K - A - B); 
		}
		System.out.println(answer);
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt(); // 1
		int B = sc.nextInt(); // 0
		int C = sc.nextInt(); // -1
		long K = sc.nextLong(); // 取るカードの枚数
		sc.close();
		long answer = 0L;
		if(K <= A) {
			answer = K;
		} else if(K <= (A + B)) {
			answer = A;
		} else {
			answer = A - (K - A - B); 
		}
		System.out.println(answer);
	}
}