import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			@SuppressWarnings("unused")
			int a = scanner.nextInt(), b = scanner.nextInt(), c = scanner.nextInt();
			int k = scanner.nextInt();
			int sum = Math.min(k, a);
			k -= Math.min(k, a);
			k -= Math.min(k, b);
			sum -= k;
			System.out.println(sum);
		}
	}
} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			@SuppressWarnings("unused")
			int a = scanner.nextInt(), b = scanner.nextInt(), c = scanner.nextInt();
			int k = scanner.nextInt();
			int sum = Math.min(k, a);
			k -= Math.min(k, a);
			k -= Math.min(k, b);
			sum -= k;
			System.out.println(sum);
		}
	}
} Main public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			@SuppressWarnings("unused")
			int a = scanner.nextInt(), b = scanner.nextInt(), c = scanner.nextInt();
			int k = scanner.nextInt();
			int sum = Math.min(k, a);
			k -= Math.min(k, a);
			k -= Math.min(k, b);
			sum -= k;
			System.out.println(sum);
		}
	} main {
		try (Scanner scanner = new Scanner(System.in)) {
			@SuppressWarnings("unused")
			int a = scanner.nextInt(), b = scanner.nextInt(), c = scanner.nextInt();
			int k = scanner.nextInt();
			int sum = Math.min(k, a);
			k -= Math.min(k, a);
			k -= Math.min(k, b);
			sum -= k;
			System.out.println(sum);
		}
	} try (Scanner scanner = new Scanner(System.in)) {
			@SuppressWarnings("unused")
			int a = scanner.nextInt(), b = scanner.nextInt(), c = scanner.nextInt();
			int k = scanner.nextInt();
			int sum = Math.min(k, a);
			k -= Math.min(k, a);
			k -= Math.min(k, b);
			sum -= k;
			System.out.println(sum);
		} {
			@SuppressWarnings("unused")
			int a = scanner.nextInt(), b = scanner.nextInt(), c = scanner.nextInt();
			int k = scanner.nextInt();
			int sum = Math.min(k, a);
			k -= Math.min(k, a);
			k -= Math.min(k, b);
			sum -= k;
			System.out.println(sum);
		} Scanner scanner = new Scanner(System.in) Scanner scanner = new Scanner(System.in) new Scanner(System.in) @SuppressWarnings("unused")
			int a = scanner.nextInt() a scanner.nextInt() scanner.nextInt scanner b = scanner.nextInt() b scanner.nextInt() scanner.nextInt scanner c = scanner.nextInt(); c scanner.nextInt() scanner.nextInt scanner int k = scanner.nextInt(); k scanner.nextInt() scanner.nextInt scanner int sum = Math.min(k, a); sum Math.min(k, a) Math.min Math k a k -= Math.min(k, a) k Math.min(k, a) Math.min Math k a k -= Math.min(k, b) k Math.min(k, b) Math.min Math k b sum -= k sum 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) {
		try (Scanner scanner = new Scanner(System.in)) {
			@SuppressWarnings("unused")
			int a = scanner.nextInt(), b = scanner.nextInt(), c = scanner.nextInt();
			int k = scanner.nextInt();
			int sum = Math.min(k, a);
			k -= Math.min(k, a);
			k -= Math.min(k, b);
			sum -= k;
			System.out.println(sum);
		}
	}
} public class Main {

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			@SuppressWarnings("unused")
			int a = scanner.nextInt(), b = scanner.nextInt(), c = scanner.nextInt();
			int k = scanner.nextInt();
			int sum = Math.min(k, a);
			k -= Math.min(k, a);
			k -= Math.min(k, b);
			sum -= k;
			System.out.println(sum);
		}
	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			@SuppressWarnings("unused")
			int a = scanner.nextInt(), b = scanner.nextInt(), c = scanner.nextInt();
			int k = scanner.nextInt();
			int sum = Math.min(k, a);
			k -= Math.min(k, a);
			k -= Math.min(k, b);
			sum -= k;
			System.out.println(sum);
		}
	}
}
