import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int A = 0, B = 0, C = 0, K = 0;
		int a = 0, b = 0, c = 0;
		Scanner sc = new Scanner(System.in);
		A = Integer.parseInt(sc.next());
		B = Integer.parseInt(sc.next());
		C = Integer.parseInt(sc.next());
		K = Integer.parseInt(sc.next());
		if (K - A >= 0) {
			a = A;
			K = K - A;
		} else {
			a = K;
			K = 0;
		}
		if (K - B >= 0) {
			b = B;
			K = K - B;
		} else {
			b = K;
			K = 0;
		}
		if (K - C >= 0) {
			c = C;
			K = K - C;
		} else {
			c = K;
			K = 0;
		}
		System.out.println(a + c * (-1));
	}

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

	public static void main(String[] args) {
		int A = 0, B = 0, C = 0, K = 0;
		int a = 0, b = 0, c = 0;
		Scanner sc = new Scanner(System.in);
		A = Integer.parseInt(sc.next());
		B = Integer.parseInt(sc.next());
		C = Integer.parseInt(sc.next());
		K = Integer.parseInt(sc.next());
		if (K - A >= 0) {
			a = A;
			K = K - A;
		} else {
			a = K;
			K = 0;
		}
		if (K - B >= 0) {
			b = B;
			K = K - B;
		} else {
			b = K;
			K = 0;
		}
		if (K - C >= 0) {
			c = C;
			K = K - C;
		} else {
			c = K;
			K = 0;
		}
		System.out.println(a + c * (-1));
	}

} Main public static void main(String[] args) {
		int A = 0, B = 0, C = 0, K = 0;
		int a = 0, b = 0, c = 0;
		Scanner sc = new Scanner(System.in);
		A = Integer.parseInt(sc.next());
		B = Integer.parseInt(sc.next());
		C = Integer.parseInt(sc.next());
		K = Integer.parseInt(sc.next());
		if (K - A >= 0) {
			a = A;
			K = K - A;
		} else {
			a = K;
			K = 0;
		}
		if (K - B >= 0) {
			b = B;
			K = K - B;
		} else {
			b = K;
			K = 0;
		}
		if (K - C >= 0) {
			c = C;
			K = K - C;
		} else {
			c = K;
			K = 0;
		}
		System.out.println(a + c * (-1));
	} main {
		int A = 0, B = 0, C = 0, K = 0;
		int a = 0, b = 0, c = 0;
		Scanner sc = new Scanner(System.in);
		A = Integer.parseInt(sc.next());
		B = Integer.parseInt(sc.next());
		C = Integer.parseInt(sc.next());
		K = Integer.parseInt(sc.next());
		if (K - A >= 0) {
			a = A;
			K = K - A;
		} else {
			a = K;
			K = 0;
		}
		if (K - B >= 0) {
			b = B;
			K = K - B;
		} else {
			b = K;
			K = 0;
		}
		if (K - C >= 0) {
			c = C;
			K = K - C;
		} else {
			c = K;
			K = 0;
		}
		System.out.println(a + c * (-1));
	} int A = 0 A 0 B = 0 B 0 C = 0 C 0 K = 0; K 0 int a = 0 a 0 b = 0 b 0 c = 0; c 0 Scanner sc = new Scanner(System.in); sc new Scanner(System.in) A = Integer.parseInt(sc.next()) A Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc B = Integer.parseInt(sc.next()) B Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc C = Integer.parseInt(sc.next()) C Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc K = Integer.parseInt(sc.next()) K Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc if (K - A >= 0) {
			a = A;
			K = K - A;
		} else {
			a = K;
			K = 0;
		} K - A >= 0 K - A K A 0 {
			a = A;
			K = K - A;
		} a = A a A K = K - A K K - A K A {
			a = K;
			K = 0;
		} a = K a K K = 0 K 0 if (K - B >= 0) {
			b = B;
			K = K - B;
		} else {
			b = K;
			K = 0;
		} K - B >= 0 K - B K B 0 {
			b = B;
			K = K - B;
		} b = B b B K = K - B K K - B K B {
			b = K;
			K = 0;
		} b = K b K K = 0 K 0 if (K - C >= 0) {
			c = C;
			K = K - C;
		} else {
			c = K;
			K = 0;
		} K - C >= 0 K - C K C 0 {
			c = C;
			K = K - C;
		} c = C c C K = K - C K K - C K C {
			c = K;
			K = 0;
		} c = K c K K = 0 K 0 System.out.println(a + c * (-1)) System.out.println System.out System System.out a + c * (-1) a c * (-1) c (-1) 1 String[] args args public class Main {

	public static void main(String[] args) {
		int A = 0, B = 0, C = 0, K = 0;
		int a = 0, b = 0, c = 0;
		Scanner sc = new Scanner(System.in);
		A = Integer.parseInt(sc.next());
		B = Integer.parseInt(sc.next());
		C = Integer.parseInt(sc.next());
		K = Integer.parseInt(sc.next());
		if (K - A >= 0) {
			a = A;
			K = K - A;
		} else {
			a = K;
			K = 0;
		}
		if (K - B >= 0) {
			b = B;
			K = K - B;
		} else {
			b = K;
			K = 0;
		}
		if (K - C >= 0) {
			c = C;
			K = K - C;
		} else {
			c = K;
			K = 0;
		}
		System.out.println(a + c * (-1));
	}

} public class Main {

	public static void main(String[] args) {
		int A = 0, B = 0, C = 0, K = 0;
		int a = 0, b = 0, c = 0;
		Scanner sc = new Scanner(System.in);
		A = Integer.parseInt(sc.next());
		B = Integer.parseInt(sc.next());
		C = Integer.parseInt(sc.next());
		K = Integer.parseInt(sc.next());
		if (K - A >= 0) {
			a = A;
			K = K - A;
		} else {
			a = K;
			K = 0;
		}
		if (K - B >= 0) {
			b = B;
			K = K - B;
		} else {
			b = K;
			K = 0;
		}
		if (K - C >= 0) {
			c = C;
			K = K - C;
		} else {
			c = K;
			K = 0;
		}
		System.out.println(a + c * (-1));
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int A = 0, B = 0, C = 0, K = 0;
		int a = 0, b = 0, c = 0;
		Scanner sc = new Scanner(System.in);
		A = Integer.parseInt(sc.next());
		B = Integer.parseInt(sc.next());
		C = Integer.parseInt(sc.next());
		K = Integer.parseInt(sc.next());
		if (K - A >= 0) {
			a = A;
			K = K - A;
		} else {
			a = K;
			K = 0;
		}
		if (K - B >= 0) {
			b = B;
			K = K - B;
		} else {
			b = K;
			K = 0;
		}
		if (K - C >= 0) {
			c = C;
			K = K - C;
		} else {
			c = K;
			K = 0;
		}
		System.out.println(a + c * (-1));
	}

}
