import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a[] = new int[3];
		a[0] = sc.nextInt();
		a[1] = sc.nextInt();
		a[2] = sc.nextInt();
		int K = sc.nextInt();
		sc.close();
		long sum = 0;
		if (K >= a[0]) {
			sum += a[0];
			K -= a[0];
		} else {
			sum += K;
			K = 0;
		}
		if (K >= a[1]) {
			K -= a[1];
		} else {
			K = 0;
		}
		if (K >= a[2]) {
			sum -= a[2];
			K -= a[2];
		} else {
			sum -= K;
			K = 0;
		}
		System.out.println(sum);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a[] = new int[3];
		a[0] = sc.nextInt();
		a[1] = sc.nextInt();
		a[2] = sc.nextInt();
		int K = sc.nextInt();
		sc.close();
		long sum = 0;
		if (K >= a[0]) {
			sum += a[0];
			K -= a[0];
		} else {
			sum += K;
			K = 0;
		}
		if (K >= a[1]) {
			K -= a[1];
		} else {
			K = 0;
		}
		if (K >= a[2]) {
			sum -= a[2];
			K -= a[2];
		} else {
			sum -= K;
			K = 0;
		}
		System.out.println(sum);
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a[] = new int[3];
		a[0] = sc.nextInt();
		a[1] = sc.nextInt();
		a[2] = sc.nextInt();
		int K = sc.nextInt();
		sc.close();
		long sum = 0;
		if (K >= a[0]) {
			sum += a[0];
			K -= a[0];
		} else {
			sum += K;
			K = 0;
		}
		if (K >= a[1]) {
			K -= a[1];
		} else {
			K = 0;
		}
		if (K >= a[2]) {
			sum -= a[2];
			K -= a[2];
		} else {
			sum -= K;
			K = 0;
		}
		System.out.println(sum);
	} main {
		Scanner sc = new Scanner(System.in);
		int a[] = new int[3];
		a[0] = sc.nextInt();
		a[1] = sc.nextInt();
		a[2] = sc.nextInt();
		int K = sc.nextInt();
		sc.close();
		long sum = 0;
		if (K >= a[0]) {
			sum += a[0];
			K -= a[0];
		} else {
			sum += K;
			K = 0;
		}
		if (K >= a[1]) {
			K -= a[1];
		} else {
			K = 0;
		}
		if (K >= a[2]) {
			sum -= a[2];
			K -= a[2];
		} else {
			sum -= K;
			K = 0;
		}
		System.out.println(sum);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a[] = new int[3]; a new int[3] 3 a[0] = sc.nextInt() a[0] a 0 sc.nextInt() sc.nextInt sc a[1] = sc.nextInt() a[1] a 1 sc.nextInt() sc.nextInt sc a[2] = sc.nextInt() a[2] a 2 sc.nextInt() sc.nextInt sc int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc sc.close() sc.close sc long sum = 0; sum 0 if (K >= a[0]) {
			sum += a[0];
			K -= a[0];
		} else {
			sum += K;
			K = 0;
		} K >= a[0] K a[0] a 0 {
			sum += a[0];
			K -= a[0];
		} sum += a[0] sum a[0] a 0 K -= a[0] K a[0] a 0 {
			sum += K;
			K = 0;
		} sum += K sum K K = 0 K 0 if (K >= a[1]) {
			K -= a[1];
		} else {
			K = 0;
		} K >= a[1] K a[1] a 1 {
			K -= a[1];
		} K -= a[1] K a[1] a 1 {
			K = 0;
		} K = 0 K 0 if (K >= a[2]) {
			sum -= a[2];
			K -= a[2];
		} else {
			sum -= K;
			K = 0;
		} K >= a[2] K a[2] a 2 {
			sum -= a[2];
			K -= a[2];
		} sum -= a[2] sum a[2] a 2 K -= a[2] K a[2] a 2 {
			sum -= K;
			K = 0;
		} sum -= K sum K K = 0 K 0 System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a[] = new int[3];
		a[0] = sc.nextInt();
		a[1] = sc.nextInt();
		a[2] = sc.nextInt();
		int K = sc.nextInt();
		sc.close();
		long sum = 0;
		if (K >= a[0]) {
			sum += a[0];
			K -= a[0];
		} else {
			sum += K;
			K = 0;
		}
		if (K >= a[1]) {
			K -= a[1];
		} else {
			K = 0;
		}
		if (K >= a[2]) {
			sum -= a[2];
			K -= a[2];
		} else {
			sum -= K;
			K = 0;
		}
		System.out.println(sum);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a[] = new int[3];
		a[0] = sc.nextInt();
		a[1] = sc.nextInt();
		a[2] = sc.nextInt();
		int K = sc.nextInt();
		sc.close();
		long sum = 0;
		if (K >= a[0]) {
			sum += a[0];
			K -= a[0];
		} else {
			sum += K;
			K = 0;
		}
		if (K >= a[1]) {
			K -= a[1];
		} else {
			K = 0;
		}
		if (K >= a[2]) {
			sum -= a[2];
			K -= a[2];
		} else {
			sum -= K;
			K = 0;
		}
		System.out.println(sum);
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a[] = new int[3];
		a[0] = sc.nextInt();
		a[1] = sc.nextInt();
		a[2] = sc.nextInt();
		int K = sc.nextInt();
		sc.close();
		long sum = 0;
		if (K >= a[0]) {
			sum += a[0];
			K -= a[0];
		} else {
			sum += K;
			K = 0;
		}
		if (K >= a[1]) {
			K -= a[1];
		} else {
			K = 0;
		}
		if (K >= a[2]) {
			sum -= a[2];
			K -= a[2];
		} else {
			sum -= K;
			K = 0;
		}
		System.out.println(sum);
	}
}
