import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		
		int sum = 0;
		if(k <= a) {
			System.out.println(k);
		}else {
			for(int i = 0; i < a; i++) {
				sum++;
			}
			
			k = k - a - b;
			if(k <= 0) {
				System.out.println(sum);
			}else {
				System.out.println(sum - k);
			}
			
		}
		
		
	}

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

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		
		int sum = 0;
		if(k <= a) {
			System.out.println(k);
		}else {
			for(int i = 0; i < a; i++) {
				sum++;
			}
			
			k = k - a - b;
			if(k <= 0) {
				System.out.println(sum);
			}else {
				System.out.println(sum - k);
			}
			
		}
		
		
	}

} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		
		int sum = 0;
		if(k <= a) {
			System.out.println(k);
		}else {
			for(int i = 0; i < a; i++) {
				sum++;
			}
			
			k = k - a - b;
			if(k <= 0) {
				System.out.println(sum);
			}else {
				System.out.println(sum - k);
			}
			
		}
		
		
	} main {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		
		int sum = 0;
		if(k <= a) {
			System.out.println(k);
		}else {
			for(int i = 0; i < a; i++) {
				sum++;
			}
			
			k = k - a - b;
			if(k <= 0) {
				System.out.println(sum);
			}else {
				System.out.println(sum - 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 sum = 0; sum 0 if(k <= a) {
			System.out.println(k);
		}else {
			for(int i = 0; i < a; i++) {
				sum++;
			}
			
			k = k - a - b;
			if(k <= 0) {
				System.out.println(sum);
			}else {
				System.out.println(sum - k);
			}
			
		} k <= a k a {
			System.out.println(k);
		} System.out.println(k) System.out.println System.out System System.out k {
			for(int i = 0; i < a; i++) {
				sum++;
			}
			
			k = k - a - b;
			if(k <= 0) {
				System.out.println(sum);
			}else {
				System.out.println(sum - k);
			}
			
		} for(int i = 0; i < a; i++) {
				sum++;
			} int i = 0; int i = 0; i 0 i < a i a i++ i++ i {
				sum++;
			} {
				sum++;
			} sum++ sum k = k - a - b k k - a - b k - a - b k a b if(k <= 0) {
				System.out.println(sum);
			}else {
				System.out.println(sum - k);
			} k <= 0 k 0 {
				System.out.println(sum);
			} System.out.println(sum) System.out.println System.out System System.out sum {
				System.out.println(sum - k);
			} System.out.println(sum - k) System.out.println System.out System System.out sum - k sum k String[] args args public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		
		int sum = 0;
		if(k <= a) {
			System.out.println(k);
		}else {
			for(int i = 0; i < a; i++) {
				sum++;
			}
			
			k = k - a - b;
			if(k <= 0) {
				System.out.println(sum);
			}else {
				System.out.println(sum - k);
			}
			
		}
		
		
	}

} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		
		int sum = 0;
		if(k <= a) {
			System.out.println(k);
		}else {
			for(int i = 0; i < a; i++) {
				sum++;
			}
			
			k = k - a - b;
			if(k <= 0) {
				System.out.println(sum);
			}else {
				System.out.println(sum - k);
			}
			
		}
		
		
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
		
		int sum = 0;
		if(k <= a) {
			System.out.println(k);
		}else {
			for(int i = 0; i < a; i++) {
				sum++;
			}
			
			k = k - a - b;
			if(k <= 0) {
				System.out.println(sum);
			}else {
				System.out.println(sum - k);
			}
			
		}
		
		
	}

}
