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;
		while (K != 0 && a[0] > 0) {
			sum += 1;
			--K;
			a[0]--;
		}
		while (K != 0 && a[1] > 0) {
			a[1]--;
			--K;
		}
		while (K != 0 && a[2] > 0) {
			sum -= 1;
			a[2]--;
			--K;
		}
		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;
		while (K != 0 && a[0] > 0) {
			sum += 1;
			--K;
			a[0]--;
		}
		while (K != 0 && a[1] > 0) {
			a[1]--;
			--K;
		}
		while (K != 0 && a[2] > 0) {
			sum -= 1;
			a[2]--;
			--K;
		}
		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;
		while (K != 0 && a[0] > 0) {
			sum += 1;
			--K;
			a[0]--;
		}
		while (K != 0 && a[1] > 0) {
			a[1]--;
			--K;
		}
		while (K != 0 && a[2] > 0) {
			sum -= 1;
			a[2]--;
			--K;
		}
		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;
		while (K != 0 && a[0] > 0) {
			sum += 1;
			--K;
			a[0]--;
		}
		while (K != 0 && a[1] > 0) {
			a[1]--;
			--K;
		}
		while (K != 0 && a[2] > 0) {
			sum -= 1;
			a[2]--;
			--K;
		}
		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 while (K != 0 && a[0] > 0) {
			sum += 1;
			--K;
			a[0]--;
		} K != 0 && a[0] > 0 K != 0 K 0 a[0] > 0 a[0] a 0 0 {
			sum += 1;
			--K;
			a[0]--;
		} sum += 1 sum 1 --K K a[0]-- a[0] a 0 while (K != 0 && a[1] > 0) {
			a[1]--;
			--K;
		} K != 0 && a[1] > 0 K != 0 K 0 a[1] > 0 a[1] a 1 0 {
			a[1]--;
			--K;
		} a[1]-- a[1] a 1 --K K while (K != 0 && a[2] > 0) {
			sum -= 1;
			a[2]--;
			--K;
		} K != 0 && a[2] > 0 K != 0 K 0 a[2] > 0 a[2] a 2 0 {
			sum -= 1;
			a[2]--;
			--K;
		} sum -= 1 sum 1 a[2]-- a[2] a 2 --K K 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;
		while (K != 0 && a[0] > 0) {
			sum += 1;
			--K;
			a[0]--;
		}
		while (K != 0 && a[1] > 0) {
			a[1]--;
			--K;
		}
		while (K != 0 && a[2] > 0) {
			sum -= 1;
			a[2]--;
			--K;
		}
		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;
		while (K != 0 && a[0] > 0) {
			sum += 1;
			--K;
			a[0]--;
		}
		while (K != 0 && a[1] > 0) {
			a[1]--;
			--K;
		}
		while (K != 0 && a[2] > 0) {
			sum -= 1;
			a[2]--;
			--K;
		}
		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;
		while (K != 0 && a[0] > 0) {
			sum += 1;
			--K;
			a[0]--;
		}
		while (K != 0 && a[1] > 0) {
			a[1]--;
			--K;
		}
		while (K != 0 && a[2] > 0) {
			sum -= 1;
			a[2]--;
			--K;
		}
		System.out.println(sum);
	}
}
