import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();
		int ans = 0;
		
		if(A < K) {
			for(int i=0;i<A;i++) {
				ans++;
			}
		}else {
			for(int j=0;j<K;j++) {
				ans++;
			}
		}
		if(B < K-A) {
			for(int i=0;i<K-A-B;i++) {
				ans--;
			}
		}
		System.out.println(ans);
	}

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

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();
		int ans = 0;
		
		if(A < K) {
			for(int i=0;i<A;i++) {
				ans++;
			}
		}else {
			for(int j=0;j<K;j++) {
				ans++;
			}
		}
		if(B < K-A) {
			for(int i=0;i<K-A-B;i++) {
				ans--;
			}
		}
		System.out.println(ans);
	}

} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();
		int ans = 0;
		
		if(A < K) {
			for(int i=0;i<A;i++) {
				ans++;
			}
		}else {
			for(int j=0;j<K;j++) {
				ans++;
			}
		}
		if(B < K-A) {
			for(int i=0;i<K-A-B;i++) {
				ans--;
			}
		}
		System.out.println(ans);
	} main {
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();
		int ans = 0;
		
		if(A < K) {
			for(int i=0;i<A;i++) {
				ans++;
			}
		}else {
			for(int j=0;j<K;j++) {
				ans++;
			}
		}
		if(B < K-A) {
			for(int i=0;i<K-A-B;i++) {
				ans--;
			}
		}
		System.out.println(ans);
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int A = scan.nextInt(); A scan.nextInt() scan.nextInt scan int B = scan.nextInt(); B scan.nextInt() scan.nextInt scan int C = scan.nextInt(); C scan.nextInt() scan.nextInt scan int K = scan.nextInt(); K scan.nextInt() scan.nextInt scan int ans = 0; ans 0 if(A < K) {
			for(int i=0;i<A;i++) {
				ans++;
			}
		}else {
			for(int j=0;j<K;j++) {
				ans++;
			}
		} A < K A K {
			for(int i=0;i<A;i++) {
				ans++;
			}
		} for(int i=0;i<A;i++) {
				ans++;
			} int i=0; int i=0; i 0 i<A i A i++ i++ i {
				ans++;
			} {
				ans++;
			} ans++ ans {
			for(int j=0;j<K;j++) {
				ans++;
			}
		} for(int j=0;j<K;j++) {
				ans++;
			} int j=0; int j=0; j 0 j<K j K j++ j++ j {
				ans++;
			} {
				ans++;
			} ans++ ans if(B < K-A) {
			for(int i=0;i<K-A-B;i++) {
				ans--;
			}
		} B < K-A B K-A K A {
			for(int i=0;i<K-A-B;i++) {
				ans--;
			}
		} for(int i=0;i<K-A-B;i++) {
				ans--;
			} int i=0; int i=0; i 0 i<K-A-B i K-A-B K-A-B K A B i++ i++ i {
				ans--;
			} {
				ans--;
			} ans-- ans System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();
		int ans = 0;
		
		if(A < K) {
			for(int i=0;i<A;i++) {
				ans++;
			}
		}else {
			for(int j=0;j<K;j++) {
				ans++;
			}
		}
		if(B < K-A) {
			for(int i=0;i<K-A-B;i++) {
				ans--;
			}
		}
		System.out.println(ans);
	}

} public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();
		int ans = 0;
		
		if(A < K) {
			for(int i=0;i<A;i++) {
				ans++;
			}
		}else {
			for(int j=0;j<K;j++) {
				ans++;
			}
		}
		if(B < K-A) {
			for(int i=0;i<K-A-B;i++) {
				ans--;
			}
		}
		System.out.println(ans);
	}

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

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();
		int ans = 0;
		
		if(A < K) {
			for(int i=0;i<A;i++) {
				ans++;
			}
		}else {
			for(int j=0;j<K;j++) {
				ans++;
			}
		}
		if(B < K-A) {
			for(int i=0;i<K-A-B;i++) {
				ans--;
			}
		}
		System.out.println(ans);
	}

}
