import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		int score = 0;
		if (k < a) {
			score = k;
		} else {
			score = a;
			k -= a;
			if (k > b) { // k <= b, do nothing, too.
				k -= b;
				score -= k;
			}
		}
		System.out.println(score);
	}

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

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		int score = 0;
		if (k < a) {
			score = k;
		} else {
			score = a;
			k -= a;
			if (k > b) { // k <= b, do nothing, too.
				k -= b;
				score -= k;
			}
		}
		System.out.println(score);
	}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		int score = 0;
		if (k < a) {
			score = k;
		} else {
			score = a;
			k -= a;
			if (k > b) { // k <= b, do nothing, too.
				k -= b;
				score -= k;
			}
		}
		System.out.println(score);
	} main {
		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		int score = 0;
		if (k < a) {
			score = k;
		} else {
			score = a;
			k -= a;
			if (k > b) { // k <= b, do nothing, too.
				k -= b;
				score -= k;
			}
		}
		System.out.println(score);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc int b = sc.nextInt(); b sc.nextInt() sc.nextInt sc int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int score = 0; score 0 if (k < a) {
			score = k;
		} else {
			score = a;
			k -= a;
			if (k > b) { // k <= b, do nothing, too.
				k -= b;
				score -= k;
			}
		} k < a k a {
			score = k;
		} score = k score k {
			score = a;
			k -= a;
			if (k > b) { // k <= b, do nothing, too.
				k -= b;
				score -= k;
			}
		} score = a score a k -= a k a if (k > b) { // k <= b, do nothing, too.
				k -= b;
				score -= k;
			} k > b k b { // k <= b, do nothing, too.
				k -= b;
				score -= k;
			} k -= b k b score -= k score k System.out.println(score) System.out.println System.out System System.out score String[] args args public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		int score = 0;
		if (k < a) {
			score = k;
		} else {
			score = a;
			k -= a;
			if (k > b) { // k <= b, do nothing, too.
				k -= b;
				score -= k;
			}
		}
		System.out.println(score);
	}

} public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		int score = 0;
		if (k < a) {
			score = k;
		} else {
			score = a;
			k -= a;
			if (k > b) { // k <= b, do nothing, too.
				k -= b;
				score -= k;
			}
		}
		System.out.println(score);
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();

		int score = 0;
		if (k < a) {
			score = k;
		} else {
			score = a;
			k -= a;
			if (k > b) { // k <= b, do nothing, too.
				k -= b;
				score -= k;
			}
		}
		System.out.println(score);
	}

}