import java.util.*;


public class Main {

	public static void main(String[] args) {
		Scanner scanner =new Scanner(System.in);
		String line1 = scanner.nextLine();
		scanner.close();
		String[] linestr =line1.split(" ",0);
		long cards = Long.parseLong(linestr[3]);
		long ans = 0;
		long[] cardsArr = new long[3];
		long[] ansArr = new long[3];
		
		for (int i = 0; i < 3; i++) {
			cardsArr[i] += Long.parseLong(linestr[i]);
		}

		for (int i = 0; i < 3; i++) {
			if (cards - cardsArr[i] >= 0) {
				cards = cards - cardsArr[i];
				ansArr[i] = cardsArr[i];
			} else {
				ansArr[i] = cards;
				break;
			}
		}
		ans = ansArr[0] * 1 + ansArr[1] * 0 + ansArr[2] * -1;
		System.out.println(ans);

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

	public static void main(String[] args) {
		Scanner scanner =new Scanner(System.in);
		String line1 = scanner.nextLine();
		scanner.close();
		String[] linestr =line1.split(" ",0);
		long cards = Long.parseLong(linestr[3]);
		long ans = 0;
		long[] cardsArr = new long[3];
		long[] ansArr = new long[3];
		
		for (int i = 0; i < 3; i++) {
			cardsArr[i] += Long.parseLong(linestr[i]);
		}

		for (int i = 0; i < 3; i++) {
			if (cards - cardsArr[i] >= 0) {
				cards = cards - cardsArr[i];
				ansArr[i] = cardsArr[i];
			} else {
				ansArr[i] = cards;
				break;
			}
		}
		ans = ansArr[0] * 1 + ansArr[1] * 0 + ansArr[2] * -1;
		System.out.println(ans);

	}
} Main public static void main(String[] args) {
		Scanner scanner =new Scanner(System.in);
		String line1 = scanner.nextLine();
		scanner.close();
		String[] linestr =line1.split(" ",0);
		long cards = Long.parseLong(linestr[3]);
		long ans = 0;
		long[] cardsArr = new long[3];
		long[] ansArr = new long[3];
		
		for (int i = 0; i < 3; i++) {
			cardsArr[i] += Long.parseLong(linestr[i]);
		}

		for (int i = 0; i < 3; i++) {
			if (cards - cardsArr[i] >= 0) {
				cards = cards - cardsArr[i];
				ansArr[i] = cardsArr[i];
			} else {
				ansArr[i] = cards;
				break;
			}
		}
		ans = ansArr[0] * 1 + ansArr[1] * 0 + ansArr[2] * -1;
		System.out.println(ans);

	} main {
		Scanner scanner =new Scanner(System.in);
		String line1 = scanner.nextLine();
		scanner.close();
		String[] linestr =line1.split(" ",0);
		long cards = Long.parseLong(linestr[3]);
		long ans = 0;
		long[] cardsArr = new long[3];
		long[] ansArr = new long[3];
		
		for (int i = 0; i < 3; i++) {
			cardsArr[i] += Long.parseLong(linestr[i]);
		}

		for (int i = 0; i < 3; i++) {
			if (cards - cardsArr[i] >= 0) {
				cards = cards - cardsArr[i];
				ansArr[i] = cardsArr[i];
			} else {
				ansArr[i] = cards;
				break;
			}
		}
		ans = ansArr[0] * 1 + ansArr[1] * 0 + ansArr[2] * -1;
		System.out.println(ans);

	} Scanner scanner =new Scanner(System.in); scanner new Scanner(System.in) String line1 = scanner.nextLine(); line1 scanner.nextLine() scanner.nextLine scanner scanner.close() scanner.close scanner String[] linestr =line1.split(" ",0); linestr line1.split(" ",0) line1.split line1 " " 0 long cards = Long.parseLong(linestr[3]); cards Long.parseLong(linestr[3]) Long.parseLong Long linestr[3] linestr 3 long ans = 0; ans 0 long[] cardsArr = new long[3]; cardsArr new long[3] 3 long[] ansArr = new long[3]; ansArr new long[3] 3 for (int i = 0; i < 3; i++) {
			cardsArr[i] += Long.parseLong(linestr[i]);
		} int i = 0; int i = 0; i 0 i < 3 i 3 i++ i++ i {
			cardsArr[i] += Long.parseLong(linestr[i]);
		} {
			cardsArr[i] += Long.parseLong(linestr[i]);
		} cardsArr[i] += Long.parseLong(linestr[i]) cardsArr[i] cardsArr i Long.parseLong(linestr[i]) Long.parseLong Long linestr[i] linestr i for (int i = 0; i < 3; i++) {
			if (cards - cardsArr[i] >= 0) {
				cards = cards - cardsArr[i];
				ansArr[i] = cardsArr[i];
			} else {
				ansArr[i] = cards;
				break;
			}
		} int i = 0; int i = 0; i 0 i < 3 i 3 i++ i++ i {
			if (cards - cardsArr[i] >= 0) {
				cards = cards - cardsArr[i];
				ansArr[i] = cardsArr[i];
			} else {
				ansArr[i] = cards;
				break;
			}
		} {
			if (cards - cardsArr[i] >= 0) {
				cards = cards - cardsArr[i];
				ansArr[i] = cardsArr[i];
			} else {
				ansArr[i] = cards;
				break;
			}
		} if (cards - cardsArr[i] >= 0) {
				cards = cards - cardsArr[i];
				ansArr[i] = cardsArr[i];
			} else {
				ansArr[i] = cards;
				break;
			} cards - cardsArr[i] >= 0 cards - cardsArr[i] cards cardsArr[i] cardsArr i 0 {
				cards = cards - cardsArr[i];
				ansArr[i] = cardsArr[i];
			} cards = cards - cardsArr[i] cards cards - cardsArr[i] cards cardsArr[i] cardsArr i ansArr[i] = cardsArr[i] ansArr[i] ansArr i cardsArr[i] cardsArr i {
				ansArr[i] = cards;
				break;
			} ansArr[i] = cards ansArr[i] ansArr i cards break; ans = ansArr[0] * 1 + ansArr[1] * 0 + ansArr[2] * -1 ans ansArr[0] * 1 + ansArr[1] * 0 + ansArr[2] * -1 ansArr[0] * 1 + ansArr[1] * 0 + ansArr[2] * -1 ansArr[0] * 1 ansArr[0] ansArr 0 1 ansArr[1] * 0 ansArr[1] ansArr 1 0 ansArr[2] * -1 ansArr[2] ansArr 2 -1 1 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 scanner =new Scanner(System.in);
		String line1 = scanner.nextLine();
		scanner.close();
		String[] linestr =line1.split(" ",0);
		long cards = Long.parseLong(linestr[3]);
		long ans = 0;
		long[] cardsArr = new long[3];
		long[] ansArr = new long[3];
		
		for (int i = 0; i < 3; i++) {
			cardsArr[i] += Long.parseLong(linestr[i]);
		}

		for (int i = 0; i < 3; i++) {
			if (cards - cardsArr[i] >= 0) {
				cards = cards - cardsArr[i];
				ansArr[i] = cardsArr[i];
			} else {
				ansArr[i] = cards;
				break;
			}
		}
		ans = ansArr[0] * 1 + ansArr[1] * 0 + ansArr[2] * -1;
		System.out.println(ans);

	}
} public class Main {

	public static void main(String[] args) {
		Scanner scanner =new Scanner(System.in);
		String line1 = scanner.nextLine();
		scanner.close();
		String[] linestr =line1.split(" ",0);
		long cards = Long.parseLong(linestr[3]);
		long ans = 0;
		long[] cardsArr = new long[3];
		long[] ansArr = new long[3];
		
		for (int i = 0; i < 3; i++) {
			cardsArr[i] += Long.parseLong(linestr[i]);
		}

		for (int i = 0; i < 3; i++) {
			if (cards - cardsArr[i] >= 0) {
				cards = cards - cardsArr[i];
				ansArr[i] = cardsArr[i];
			} else {
				ansArr[i] = cards;
				break;
			}
		}
		ans = ansArr[0] * 1 + ansArr[1] * 0 + ansArr[2] * -1;
		System.out.println(ans);

	}
} Main
import java.util.*;


public class Main {

	public static void main(String[] args) {
		Scanner scanner =new Scanner(System.in);
		String line1 = scanner.nextLine();
		scanner.close();
		String[] linestr =line1.split(" ",0);
		long cards = Long.parseLong(linestr[3]);
		long ans = 0;
		long[] cardsArr = new long[3];
		long[] ansArr = new long[3];
		
		for (int i = 0; i < 3; i++) {
			cardsArr[i] += Long.parseLong(linestr[i]);
		}

		for (int i = 0; i < 3; i++) {
			if (cards - cardsArr[i] >= 0) {
				cards = cards - cardsArr[i];
				ansArr[i] = cardsArr[i];
			} else {
				ansArr[i] = cards;
				break;
			}
		}
		ans = ansArr[0] * 1 + ansArr[1] * 0 + ansArr[2] * -1;
		System.out.println(ans);

	}
}
