import java.util.*;
public class Main {
	public static void main(String arg[]) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();

		int res = 0;

		res += (K >= A) ? A : K;
		K -= A;
		if(K > 0) {
			K-= B;
		}
		if(K > 0) {
			res -= (K >= C) ? C : K;
		}
		System.out.println(res);
	}
} import java.util.*; util.* java public class Main {
	public static void main(String arg[]) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();

		int res = 0;

		res += (K >= A) ? A : K;
		K -= A;
		if(K > 0) {
			K-= B;
		}
		if(K > 0) {
			res -= (K >= C) ? C : K;
		}
		System.out.println(res);
	}
} Main public static void main(String arg[]) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();

		int res = 0;

		res += (K >= A) ? A : K;
		K -= A;
		if(K > 0) {
			K-= B;
		}
		if(K > 0) {
			res -= (K >= C) ? C : K;
		}
		System.out.println(res);
	} main {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();

		int res = 0;

		res += (K >= A) ? A : K;
		K -= A;
		if(K > 0) {
			K-= B;
		}
		if(K > 0) {
			res -= (K >= C) ? C : K;
		}
		System.out.println(res);
	} 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 res = 0; res 0 res += (K >= A) ? A : K res (K >= A) ? A : K (K >= A) K A A K K -= A K A if(K > 0) {
			K-= B;
		} K > 0 K 0 {
			K-= B;
		} K-= B K B if(K > 0) {
			res -= (K >= C) ? C : K;
		} K > 0 K 0 {
			res -= (K >= C) ? C : K;
		} res -= (K >= C) ? C : K res (K >= C) ? C : K (K >= C) K C C K System.out.println(res) System.out.println System.out System System.out res String arg[] arg public class Main {
	public static void main(String arg[]) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();

		int res = 0;

		res += (K >= A) ? A : K;
		K -= A;
		if(K > 0) {
			K-= B;
		}
		if(K > 0) {
			res -= (K >= C) ? C : K;
		}
		System.out.println(res);
	}
} public class Main {
	public static void main(String arg[]) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();

		int res = 0;

		res += (K >= A) ? A : K;
		K -= A;
		if(K > 0) {
			K-= B;
		}
		if(K > 0) {
			res -= (K >= C) ? C : K;
		}
		System.out.println(res);
	}
} Main
import java.util.*;
public class Main {
	public static void main(String arg[]) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();

		int res = 0;

		res += (K >= A) ? A : K;
		K -= A;
		if(K > 0) {
			K-= B;
		}
		if(K > 0) {
			res -= (K >= C) ? C : K;
		}
		System.out.println(res);
	}
}