import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner stdIn = new Scanner(System.in);
		int A = stdIn.nextInt();
		int B = stdIn.nextInt();
		int C = stdIn.nextInt();
		int K = stdIn.nextInt();
		int ans = 0;
		for(int a = 0; a < K; a++){
			if(A > 0){
				A = A - 1;
				ans++;
			}
			else if(B > 0){
				B = B - 1;
			}
			else{
				C = C - 1;
				ans = ans - 1;
			}
		}
		System.out.println(ans);
		stdIn.close();
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args){
		Scanner stdIn = new Scanner(System.in);
		int A = stdIn.nextInt();
		int B = stdIn.nextInt();
		int C = stdIn.nextInt();
		int K = stdIn.nextInt();
		int ans = 0;
		for(int a = 0; a < K; a++){
			if(A > 0){
				A = A - 1;
				ans++;
			}
			else if(B > 0){
				B = B - 1;
			}
			else{
				C = C - 1;
				ans = ans - 1;
			}
		}
		System.out.println(ans);
		stdIn.close();
	}
} Main public static void main(String[] args){
		Scanner stdIn = new Scanner(System.in);
		int A = stdIn.nextInt();
		int B = stdIn.nextInt();
		int C = stdIn.nextInt();
		int K = stdIn.nextInt();
		int ans = 0;
		for(int a = 0; a < K; a++){
			if(A > 0){
				A = A - 1;
				ans++;
			}
			else if(B > 0){
				B = B - 1;
			}
			else{
				C = C - 1;
				ans = ans - 1;
			}
		}
		System.out.println(ans);
		stdIn.close();
	} main {
		Scanner stdIn = new Scanner(System.in);
		int A = stdIn.nextInt();
		int B = stdIn.nextInt();
		int C = stdIn.nextInt();
		int K = stdIn.nextInt();
		int ans = 0;
		for(int a = 0; a < K; a++){
			if(A > 0){
				A = A - 1;
				ans++;
			}
			else if(B > 0){
				B = B - 1;
			}
			else{
				C = C - 1;
				ans = ans - 1;
			}
		}
		System.out.println(ans);
		stdIn.close();
	} Scanner stdIn = new Scanner(System.in); stdIn new Scanner(System.in) int A = stdIn.nextInt(); A stdIn.nextInt() stdIn.nextInt stdIn int B = stdIn.nextInt(); B stdIn.nextInt() stdIn.nextInt stdIn int C = stdIn.nextInt(); C stdIn.nextInt() stdIn.nextInt stdIn int K = stdIn.nextInt(); K stdIn.nextInt() stdIn.nextInt stdIn int ans = 0; ans 0 for(int a = 0; a < K; a++){
			if(A > 0){
				A = A - 1;
				ans++;
			}
			else if(B > 0){
				B = B - 1;
			}
			else{
				C = C - 1;
				ans = ans - 1;
			}
		} int a = 0; int a = 0; a 0 a < K a K a++ a++ a {
			if(A > 0){
				A = A - 1;
				ans++;
			}
			else if(B > 0){
				B = B - 1;
			}
			else{
				C = C - 1;
				ans = ans - 1;
			}
		} {
			if(A > 0){
				A = A - 1;
				ans++;
			}
			else if(B > 0){
				B = B - 1;
			}
			else{
				C = C - 1;
				ans = ans - 1;
			}
		} if(A > 0){
				A = A - 1;
				ans++;
			}
			else if(B > 0){
				B = B - 1;
			}
			else{
				C = C - 1;
				ans = ans - 1;
			} A > 0 A 0 {
				A = A - 1;
				ans++;
			} A = A - 1 A A - 1 A 1 ans++ ans if(B > 0){
				B = B - 1;
			}
			else{
				C = C - 1;
				ans = ans - 1;
			} B > 0 B 0 {
				B = B - 1;
			} B = B - 1 B B - 1 B 1 {
				C = C - 1;
				ans = ans - 1;
			} C = C - 1 C C - 1 C 1 ans = ans - 1 ans ans - 1 ans 1 System.out.println(ans) System.out.println System.out System System.out ans stdIn.close() stdIn.close stdIn String[] args args public class Main {
	public static void main(String[] args){
		Scanner stdIn = new Scanner(System.in);
		int A = stdIn.nextInt();
		int B = stdIn.nextInt();
		int C = stdIn.nextInt();
		int K = stdIn.nextInt();
		int ans = 0;
		for(int a = 0; a < K; a++){
			if(A > 0){
				A = A - 1;
				ans++;
			}
			else if(B > 0){
				B = B - 1;
			}
			else{
				C = C - 1;
				ans = ans - 1;
			}
		}
		System.out.println(ans);
		stdIn.close();
	}
} public class Main {
	public static void main(String[] args){
		Scanner stdIn = new Scanner(System.in);
		int A = stdIn.nextInt();
		int B = stdIn.nextInt();
		int C = stdIn.nextInt();
		int K = stdIn.nextInt();
		int ans = 0;
		for(int a = 0; a < K; a++){
			if(A > 0){
				A = A - 1;
				ans++;
			}
			else if(B > 0){
				B = B - 1;
			}
			else{
				C = C - 1;
				ans = ans - 1;
			}
		}
		System.out.println(ans);
		stdIn.close();
	}
} Main
import java.util.Scanner;

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